1.导入pom文件
<!--<dependency>-->
<!--<groupId>javax.servlet</groupId>-->
<!--<artifactId>javax.servlet-api</artifactId>-->
<!--<version>3.0.1</version>-->
<!--<scope>provided</scope>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>javax.servlet.jsp</groupId>-->
<!--<artifactId>jsp-api</artifactId>-->
<!--<version>2.1</version>-->
<!--<scope>provided</scope>-->
<!--</dependency>-->
<!--</dependencies>
2.webapp下的web.xml配置
<!--<servlet-name>UserServlet</servlet-name>-->
继承servlet类路径
<!--<servlet-class>DeaoServlet</servlet-class>-->
<!--</servlet>-->
<!--<servlet-mapping>-->
servlet-name后台访问请求路径
<!--<servlet-name>UserServlet</servlet-name>-->
<!--<url-pattern>/UserServlet</url-pattern>-->
<!--</servlet-mapping>-->
3编写一个类继承Servlet
public class DeaoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(“text/html”);
request.setCharacterEncoding(“UTF-8”);
String username=request.getParameter(“name”);
String password=request.getParameter(“password”);
if(“小明”.equals(username) && “xiaoming”.equals(password)){
response.sendRedirect(“index.jsp?name=”+username);
}else{
request.setAttribute(“message”,“用户名或者密码错误”);
request.getRequestDispatcher(“login.jsp”).forward(request,response);
}
}
}