我将项目部署在服务器上,并且可以在index.jsp
上正常运行:
但是当页面需要.java
文件时,它会显示404。
Servletvisitor.java :
package Controller;
import Model.Blog;
import Model.UserService;
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 java.io.IOException;
import java.util.List;
@WebServlet(name = "ServletVisitor", urlPatterns = "/ServletVisitor")
public class ServletVisitor extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Blog> blogs = UserService.showBlog();
req.setAttribute("blogs", blogs);
req.getRequestDispatcher("visitor.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
index.jsp
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎</title>
</head>
<body>
<div align="center">
<h1>登录</h1>
<%
List<String> info = (List<String>) request.getAttribute("info"); //得到结果
String s;
//若用户有登录操作,则信息不为空
if(info != null) {
Iterator<String> iterator = info.iterator();
while(iterator.hasNext()) {
s = iterator.next();
//若登录成功则跳转到首页
if (s.equals("登录成功")) {
response.sendRedirect("/ServletVIP");
}
//登录不成功提示错误
else {
%>
<%--显示错误--%>
<h4><%=s%></h4>
<%
}
}
}
%>
<form action="/ServletLogin" method="post">
账号:<input type="text" name="account"><br>
<br>
密码:<input type="password" name="password"><br>
<br>
<input type="submit" value="登录">
</form>
<a href="register.jsp">还不是会员?</a><br>
<br>
<a href="/ServletVisitor">以游客身份访问</a>
</div>
</body>
</html>
我的项目结构:
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">
<context-param>
<param-name>JDBC_DRIVER</param-name>
<param-value>com.mysql.cj.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>DB_URL</param-name>
<param-value>jdbc:mysql://localhost/account?serverTimezone=GMT%2B8&useSSL=false</param-value>
</context-param>
<context-param>
<param-name>USER</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>PASS</param-name>
<param-value>password</param-value>
</context-param>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
<default-content-type>text/html</default-content-type>
</jsp-property-group>
</jsp-config>
该项目在localhost上运行良好。
我认为该错误与path
有关,但我不知道如何解决。
答案 0 :(得分:0)
链接“!” $)?)/%$ //&$%“不会链接到您的Web应用程序” / javawebblog / *“的相同上下文路径中,而是链接到根本没有的根tomcat应用程序” /“中“ / ServletVisitor”页面。这就是为什么您因此收到错误404的原因。