使用httpServlet方法开发

使用继承HttpServlet方法开发

显示HeeloWorld以及当前日期

默认Doget方式提交


package com.wangzhi.servlet;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class MyServlet extends HttpServlet { protected void doGet(HttpServletRequest res, HttpServletResponse resp) throws ServletException, java.io.IOException { resp.getWriter().println("I‘m a student"); } protected void doPost(HttpServletRequest res, HttpServletResponse resp) throws ServletException, java.io.IOException { resp.getWriter().println("I‘m a senior student"); }}

View Code

相比较post而言安全性低

 

 

怎么使用Dopost方式?

 

login.html


<html><body><form action="/web/MyServlet" method="post"><input type="String" name="username" value="username"/><input type="submit" value="login"/></form></body></html>

View Code

MyServlet.java


package com.wangzhi.servlet;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class MyServlet extends HttpServlet { protected void doGet(HttpServletRequest res,HttpServletResponse resp) throws ServletException,java.io.IOException{ resp.getWriter().println("this doGet");}protected void doPost(HttpServletRequest res,HttpServletResponse resp) throws ServletException,java.io.IOException{ resp.getWriter().println("This is doPost"+res.getParameter("username"));}}

View Code

 

 

相关文章