1.新建一个Dynamic Web Project项目,里面新建两个java文件和一个jsp文件,整体的框架如下所示: 2.login .jsp里面的代码如下所示: %@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding=
1.新建一个Dynamic Web Project项目,里面新建两个java文件和一个jsp文件,整体的框架如下所示:
2.login.jsp里面的代码如下所示:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <form action="login" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="user" /></td> </tr> <tr> <td><input type="submit" value="提交" /></td> </tr> </table> </form> </body> </html>
3. GreetServlet.java文件里面的代码如下所示:
package servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "greet", urlPatterns = { "/greet" }) public class GreetServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override public void init(ServletConfig config) throws ServletException { super.init(config); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); request.setCharacterEncoding("utf-8"); String name = request.getParameter("user"); if (name == null || name.trim().length() == 0) { response.sendRedirect("login.jsp"); } else { out.println("<html><body>"); out.println("<br>您的姓名是:" + name); out.println("<br><a href=login.jsp" + ">重新登录</a>"); out.println("</html></body>"); } } }
4. LoginServlet.java文件里面的代码如下所示:
package servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "login", urlPatterns = { "/login" }) public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override public void init(ServletConfig config) throws ServletException { super.init(config); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); request.setCharacterEncoding("utf-8"); String name = request.getParameter("user"); if (name == null || name.trim().length() == 0) { response.sendRedirect("login.jsp"); } else { out.println("<html><head><title>第二个页面</title></head><body>"); out.println("<form action='greet'" + " method='post'"); out.println("<input type='hidden' name='user' value=" + name + ">"); out.println(name + "<br>"); out.println("<input type='submit' value='转向下一个页面看看'>"); out.println("</form>"); out.println("</body></html>"); } } }
5.在浏览器里面输入http://localhost:8080/Servlet/login.jsp运行得到的结果如下所示: