JavaWeb_通过xml配置文件配置servlet访问路径
Servlet两种访问路径
一、在Servlet类中通过设置@WebServlet标注访问
二、在web.xml中编写配置文件
第一种能比较直观看到Servlet路径,但当项目发布后不好再对Servlet路径进行修改
第二种把Servlet路径在web.xml中配置, 能更好对Servlet路径进行配置的修改
利用Servlet处理注册登录请求 传送门
项目结构
package com.Gary.servlet; 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 com.Gary.model.User; import com.Gary.util.DBUtil; @WebServlet("/login_do") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); User user = DBUtil.verifyAccount(username,password); if(user==null){ // out.println("登录失败,用户名或密码错误"); request.setAttribute("message","登录失败,用户名或密码错误"); request.getRequestDispatcher("login.jsp").forward(request,response); }else{ // out.println("登录成功"); int number = 0; //获取到application对象 ServletContext application = this.getServletContext(); //第一次登录用户时if不瞒住,number=0 if(application.getAttribute("userNumber")!=null){ number = (Integer)application.getAttribute("userNumber"); } //登录过一个用户后在原基础number上进行++ number++; application.setAttribute("userNumber",number); //通过seesion的setAttribute传递数据 request.getSession().setAttribute("user", user); //登录请求的转发,将数据传递给personCenter.jsp页面 request.getRequestDispatcher("personCenter.jsp").forward(request,response); } } }
package com.Gary.servlet; import java.io.IOException; 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 com.Gary.util.DBUtil; //@WebServlet("/register_do") public class RegisterServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); int age = Integer.parseInt(request.getParameter("age")); String sex = request.getParameter("sex"); boolean isSuccess = DBUtil.addUser(username,password,age,sex); //使用请求转发 if(isSuccess){ request.setAttribute("message", "注册成功,请登录"); //通过getRequestDispatcher传递路径将请求转发给login.jsp request.getRequestDispatcher("login.jsp").forward(request,response); }else{ request.setAttribute("message", "注册失败,用户名重复"); //通过getRequestDispatcher传递路径将请求转发给login.jsp request.getRequestDispatcher("register.jsp").forward(request,response); } } }
<%@page import="com.Gary.model.User"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% Object o = session.getAttribute("user"); if(o!=null){ User user = (User)o; out.println("当前登录用户:"+user.getUsername()+"<br/>"); } %> <% Object msg = request.getAttribute("message"); if(msg!=null) out.println(msg); %> 登录<hr> <form action="login_do" method="post"> 用户名:<input type="text" name="username" /><br/> 密码:<input type="password" name="password" /><br/> <input type="submit" value="登录" /> </form> </body> </html>
<%@page import="com.Gary.model.User"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% Object o = session.getAttribute("user"); if(o!=null){ User user = (User)o; out.println("当前登录用户:"+user.getUsername()+"<br/>"); } %> <% Object msg = request.getAttribute("message"); if(msg!=null) out.println(msg); %> <form action="register_do" method="post"> 用户名:<input type="text" name="username" /><br/> 密码:<input type="password" name="password" /><br/> 年龄:<input type="text" name="age" /><br/> 性别: 男<input type="radio" name="sex" value="男" />女<input type="radio" name="sex" value="女" /><br/> <input type="submit" value="注册" /> </form> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true"> <!-- 配置servlet --> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.Gary.servlet.LoginServlet</servlet-class> </servlet> <servlet> <servlet-name>RegisterServlet</servlet-name> <servlet-class>com.Gary.servlet.RegisterServlet</servlet-class> </servlet> <!-- servlet路径映射 --> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/login_do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>RegisterServlet</servlet-name> <url-pattern>/register_do</url-pattern> </servlet-mapping> </web-app>
LoginServlet.java处理login.jsp登录表单的请求
RegisterServlet.java处理register.jsp注册表单的请求
第一种
通过在Servlet上设置@WebServlet标注,容器就会自动读取当中的信息,@WebServlet告诉容器,如果请求的URL是"/xxxxxx"
LoginServlet.java中配置的路径是login_do,在register.jsp的from表单中设置action也为login_do
由LoginServlet.java处理register.jsp的from表单提交的信息
@WebServlet("/login_do") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); User user = DBUtil.verifyAccount(username,password); if(user==null){ // out.println("登录失败,用户名或密码错误"); request.setAttribute("message","登录失败,用户名或密码错误"); request.getRequestDispatcher("login.jsp").forward(request,response); }else{ // out.println("登录成功"); int number = 0; //获取到application对象 ServletContext application = this.getServletContext(); //第一次登录用户时if不瞒住,number=0 if(application.getAttribute("userNumber")!=null){ number = (Integer)application.getAttribute("userNumber"); } //登录过一个用户后在原基础number上进行++ number++; application.setAttribute("userNumber",number); //通过seesion的setAttribute传递数据 request.getSession().setAttribute("user", user); //登录请求的转发,将数据传递给personCenter.jsp页面 request.getRequestDispatcher("personCenter.jsp").forward(request,response); } } }
<form action="login_do" method="post"> 用户名:<input type="text" name="username" /><br/> 密码:<input type="password" name="password" /><br/> <input type="submit" value="登录" /> </form>
第二种
通过web.xml配置文件进行配置
<servlet-name>中填写项目中Servlet名字 【可随便填写,用作后边<servlet-mapping>映射url】
<servlet-class>中填写项目中该Servlet路径
<servlet-mapping>配置servlet-name和<url-pattern>地址映射
<!-- 配置servlet --> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.Gary.servlet.LoginServlet</servlet-class> </servlet> <servlet> <servlet-name>RegisterServlet</servlet-name> <servlet-class>com.Gary.servlet.RegisterServlet</servlet-class> </servlet> <!-- servlet路径映射 --> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/login_do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>RegisterServlet</servlet-name> <url-pattern>/register_do</url-pattern> </servlet-mapping>
将com.Gary.servlet.LoginServlet路径上的Servlet命名为LoginServlet,与后边<servlet-mapping>中的<servlet-name>LoginServlet相匹配
【按住Ctrl+鼠标左键点击<servlet-class>下的路径可直接访问对应Servlet下的路径】