1.新建一个Dynamic Web Project项目,里面新建两个servlet,新建一个jsp,整体的框架结构如下所示: 2.RedirectForwardServlet.java文件的代码如下所示: package servlet;import java.io.IOException;import javax
1.新建一个Dynamic Web Project项目,里面新建两个servlet,新建一个jsp,整体的框架结构如下所示:
2.RedirectForwardServlet.java文件的代码如下所示:
package servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; 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("/redirectForwardServlet") public class RedirectForwardServlet extends HttpServlet { private static final long serialVersionUID = 1L; @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.setCharacterEncoding("GBK"); String name = request.getParameter("user"); String password = request.getParameter("pwd"); if (name == null || name.length() == 0) { response.sendRedirect("login.jsp"); } else if (password == null || password.length() == 0) { response.sendRedirect("login.jsp"); } else if (name.length() > 0 && password.length() > 0) { RequestDispatcher dis = request.getRequestDispatcher("showServlet"); dis.forward(request, response); } } }
3.ShowServlet.java文件的代码如下所示:
package servlet; import java.io.IOException; import java.io.PrintWriter; 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("/showServlet") public class ShowServlet extends HttpServlet { private static final long serialVersionUID = 1L; @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=GBK"); PrintWriter out = response.getWriter(); request.setCharacterEncoding("GBK"); String name = request.getParameter("user"); String password = request.getParameter("pwd"); out.println("您的用户名是:" + name); out.println("<br>您的密码是:" + password); } }
4.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>login.jsp</title> </head> <body> <form action="redirectForwardServlet" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="user" /></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="pwd" /></td> </tr> <tr> <td><input type="submit" value="提交" /></td> <td><input type="reset" value="重置" /></td> </tr> </table> </form> </body> </html>
5. 在浏览器里面输入http://localhost:8080/Servlet/login.jsp运行之后得到的结果如下所示,里面的中文出现了乱码的现象: