1.新建一个Dynamic Web Project项目,里面新建两个java文件和一个jsp文件,整体的框架如下所示: 2. useSession.jsp里面的代码如下所示: %@ page language="java" contentType="text/html; charset=UTF-8"pageEnco
1.新建一个Dynamic Web Project项目,里面新建两个java文件和一个jsp文件,整体的框架如下所示:
2.useSession.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="<%=response.encodeURL("sendMyName")%>" 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. UseSessionSevlet.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; import javax.servlet.http.HttpSession; @WebServlet(name = "useSession", urlPatterns = { "/sendMyName" }) public class UseSessionSevlet 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(response.encodeRedirectURL("useSession.jsp")); } else { HttpSession session = request.getSession(true); session.setAttribute("myName", name); out.println("<html><body>"); out.println("您请求的sevlet对象是:" + getServletName()); out.println("<br>您的会话ID是:" + session.getId()); out.println("<br>单击请求另一个sevlet:"); out.println("<br><a href=" + response.encodeURL("showMyName") + ">请求另一个sevlet</a>"); out.println("</html></body>"); } } }
4. ShowNameSevlet.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; import javax.servlet.http.HttpSession; @WebServlet(name = "showName", urlPatterns = { "/showMyName" }) public class ShowNameSevlet 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(); HttpSession session = request.getSession(true); String name = (String) session.getAttribute("myName"); out.println("<html><body>"); out.println("您请求的sevlet对象是:" + getServletName()); out.println("<br>您的会话ID是:" + session.getId()); out.println("<br>您会话中存储的用户名是:" + name); out.println("</html></body>"); } }
5.在浏览器里面输入http://localhost:8080/Servlet/useSession.jsp运行得到的结果如下所示: