一。servlet
1.是什么:java程序来处理页面请求和响应
2.实现方式:
a。实现Servlet接口
b。继承HttpServlet类
3.步骤:
a.创建一个java程序实现Servlet或者继承HttpServlet类
b.修改web.xml文件
<servlet>
<!-- servlet名字,可以自定义 -->
<servlet-name>firstServlet</servlet-name>
<!-- 要执行的servlet程序的完整类名 -->
<servlet-class>com.servlet.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>firstServlet</servlet-name>
<!-- 访问该servlet的url路径。/表示根目录开始 -->
<url-pattern>/first</url-pattern>
</servlet-mapping>
c.访问:http://localhost:8888/hwua_web/first
4.第二种方式:
继承HttpServlet,重写里面的doget和doPost方法
方法中的参数:req对应request resp对应response
session的获取方式:HttpSession session = req.getSession();
5.第三种:继承HttpServlet,重写里面的doget和doPost方法
在类上写一个注解:@WebServlet("/login3")//此注解代替web.xml的配置文件
实例1:实现 Servlet 接口 + web.xml注册
LoginServlet.java
1 package com.control; 2 3 import java.io.IOException; 4 5 import javax.servlet.Servlet; 6 import javax.servlet.ServletConfig; 7 import javax.servlet.ServletContext; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 import javax.servlet.http.Cookie; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 import javax.servlet.http.HttpSession; 15 16 import com.pojo.UserInfo; 17 import com.service.IUserInfoService; 18 import com.service.UserInfoService; 19 20 public class LoginServlet implements Servlet{ 21 public void destroy() { 22 23 } 24 public ServletConfig getServletConfig() { 25 return null; 26 } 27 public String getServletInfo() { 28 return null; 29 } 30 public void init(ServletConfig arg0) throws ServletException { 31 32 } 33 public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { 34 HttpServletRequest req = (HttpServletRequest) arg0; 35 HttpServletResponse res = (HttpServletResponse) arg1; 36 //获取session 37 HttpSession session = req.getSession(); 38 39 req.setCharacterEncoding("utf-8"); 40 String username = req.getParameter("username"); 41 String password = req.getParameter("password"); 42 String isrember = req.getParameter("isremder"); 43 IUserInfoService userSer = new UserInfoService(); 44 UserInfo user = userSer.login(username, password); 45 if(user!=null){ 46 session.setAttribute("user",user); 47 //登录成功,将用户名和密码保存到cookie中 48 if("1".equals(isrember)){//记住用户名 49 Cookie cookie = new Cookie("username",user.getUser_name()); 50 cookie.setMaxAge(1000*3600*24*7);//设置cookie的有效期 51 res.addCookie(cookie);//将cookie添加到响应中发送给客户端 52 } 53 res.sendRedirect("cookie/success.jsp"); 54 }else{ 55 res.sendRedirect("cookie/login.jsp?error=true"); 56 } 57 } 58 }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>javaweb06</display-name> <servlet> <servlet-name>loginservlet</servlet-name> <servlet-class>com.control.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>loginservlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
实例2:继承HttpServlet类 + web.xml注册
LoginServlet2.java
package com.control; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginServlet2 extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //session HttpSession session = req.getSession(); //application ServletContext app = getServletContext(); } }
web.xml文件同上,只不过将
<servlet-class>com.control.LoginServlet</servlet-class>
改为
<servlet-class>com.control.LoginServlet2</servlet-class>
实例3:继承HttpServlet类 + @WebServlet注解注册,不需要在web.xml文件中注册
LoginServlet3.java
package com.control; import java.io.IOException; import javax.servlet.ServletContext; 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("/login3")//此注解代替web.xml的配置文件 public class LoginServlet3 extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //session HttpSession session = req.getSession(); //application ServletContext app = getServletContext(); } }