package com.Servlect; 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; import javax.servlet.http.HttpSession; /** * Servlet implementation class Servlet2 * session */ @WebServlet("/Servlet2") public class Servlet2 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Servlet2() { super(); // TODO Auto-generated constructor stub System.out.println("session servlect2在运行中"); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw=response.getWriter(); //档浏览器访问服务器的servlet的时候,需要检查该浏览器的session对象是否是刚刚创建的 HttpSession session=request.getSession(); //session.isNew()判断session到值是否为空 if (session.isNew()) { //session是刚刚创建的 //浏览器是第一次访问服务器 pw.print("浏览器是第一次访问服务器"); //设置三个动态数据 session.setAttribute("text1", "一等奖学金"); session.setAttribute("name", "hug"); session.setAttribute("pw", "12345"); //删除指定的数据 //session.removeAttribute("text1"); //当session中存放大量数据的时候,如果需要全部销毁,直接干掉session对象 //session.invalidate(); }else { String info=(String) session.getAttribute("text1"); String info1=(String) session.getAttribute("name"); String info2=(String) session.getAttribute("pw"); System.out.println("浏览器不是第一次访问服务器的文件内容"); System.out.println("浏览器不是第一次访问服务器的名字"); System.out.println("浏览器不是第一次访问服务器了的密码"); System.out.println("====================================================="); pw.print("<h1>浏览器不是第一次访问服务器的文件内容<hr></h1>"+info); pw.print("<h1>浏览器不是第一次访问服务器的名字<hr></h1>"+info1); pw.print("<h1>浏览器不是第一次访问服务器了的密码<hr</h1>"+info2); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.Servlect; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Demo01Servlet */ @WebServlet("/Demo01Servlet") public class Demo01Servlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Demo01Servlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取文本框中的内容 String username = request.getParameter("username"); String password = request.getParameter("password"); //首先判断账号密码是否正确 if("hug".equals(username) && "123".equals(password)){ //登录成功之后判断用户是否点击了记住我 String remember = request.getParameter("remember"); if (remember != null) { //因为单选框默认是null,选上了之后value是on //如果用户点击了,就存到Cookie中 Cookie cookie = new Cookie("username",username); //设置访问路径 cookie.setPath(request.getContextPath()+"/index.html"); //设置生存时间 cookie.setMaxAge(60); //把cookie内容传到浏览器响应头 response.addCookie(cookie); Cookie cookie1 = new Cookie("password",password); //设置访问路径 cookie1.setPath(request.getContextPath()+"/login.html"); //设置生存时间 cookie1.setMaxAge(60 * 60 * 24); //把cookie内容传到浏览器响应头 response.addCookie(cookie1); } //登录成功,跳转到成功页面 response.sendRedirect("ServlectA"); }else{ //登录失败,跳转到失败页面 System.out.println("登录失败,跳转到失败页面"); response.sendRedirect("ServlectB"); } } }
package com.Servlect; 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; /** * Servlet implementation class ServlectA */ @WebServlet("/ServlectA") public class ServlectA extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServlectA() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw=response.getWriter(); pw.print("<h1>客户:恭喜你成功登录</h1>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.Servlect; 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; /** * Servlet implementation class ServlectB */ @WebServlet("/ServlectB") public class ServlectB extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServlectB() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw=response.getWriter(); pw.print("<h1 >客户:你成功登录失败哦</h1>"); response.setHeader("refresh", "10;url=Demo01Servlet"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用户登录</title> <!--导入js文件--> <script src="js/commons.js"></script> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h2>用户登录</h2> <form action="Demo01Servlet" method="post" id="loginForm"> <table> <tr> <td width="60">用户名</td> <td><input type="text" name="username" id="username"/></td> </tr> <tr> <td>密码</td> <td><input type="password" name="password" id="password"/></td> </tr> <tr> <td>记住我</td> <!--没有value属性的前提下,点中它的值是on--> <td><input type="checkbox" name="remember"/></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="登录"/></td> </tr> </table> </form> </body> </html>
Servlect常用对象
ServletConfig:静态数据
ServletContext:共享静态数据。共享动态数据。共享文件数据。
HttpServletRequest:
HttpServletResponse:
Cookie
Session
请求转发和重定向区别:
重定向跳转浏览器地址会发生变化,
请求转发浏览器地址不会发生变化
重定向是两次请求,请求转发是一次请求
重定向属于浏览器跳转,请求转发属于服务器内部跳转
重定向跳转页面是不能发送数据,
请求转发是可以携带数据的