-1

I deploy my project on my server and it works right on the index.jsp: index.jsp

but when I click on 以游客身份访问, it shows 404: visitor

I found that I can visit jsp file without error: home register

but when the page need .java file it shows 404.

Here are my project structure deploy on my server: structure class

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>

My project structure:

projectstructure

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&amp;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>

The project works well on localhost. I think the error is about the path, but I don't know how to solve it.

Libra Tang
  • 73
  • 1
  • 2
  • 7

1 Answers1

0

The Link "!"$)?)/%$//&$%" does not link into the same context path of your webapplication "/javawebblog/*" but into root tomcat application "/" where there is no "/ServletVisitor" page. This is why you consequently get error 404.

Selaron
  • 6,105
  • 4
  • 31
  • 39