百度了相关问题
注解时用的是value属性
用的是tomcat10
可以访问login页面
RedirectServletServle
package com.lxy;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/login1")
//@WebServlet(name = "login ",urlPatterns = "/login1")
public class RedirectServletServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
// String name = request.getParameter("username");
// String word = request.getParameter("password");
// request.setAttribute("n", name);
try {
ServletOutputStream out = response.getOutputStream();
out.print("内容");
out.println("<html><head><title></title></head><body>heelo world!!!</body></html>");
} catch (IOException e) {
e.printStackTrace();
}
// if(name.equals("lxy")&&word.equals("123456")){
// response.sendRedirect("weclome.jsp");//重定向“/”表示根目录,绝对路径
// }else {
// response.sendRedirect("error.jsp");//相对路径
// }
}
}
目录情况
login.jsp
<%@ page pageEncoding="UTF-8" import="java.util.*" language="java" %>
<% String path=request.getContextPath();%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户登录</title>
</head>
<body>
<center>
<p>用户登录</p>
</center>
<form action="login1" method="post">
<table border="1" width="250px;" align="center">
<tr>
<td width="75px">用户名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td width="75px">密 码:</td>
<td><input name="pwd" type="password"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登录"/>
</td>
</tr>
</table>
</form>
</body>
</html>
用注解的时候tomcat的日志信息报404错
用web.xml注解是报的500(可能是servlet-class的问题)
这是web.xml
<?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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>ok</servlet-name>
<servlet-class>com.lxy.RedirectServletServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ok</servlet-name>
<url-pattern>/login1</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>