Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法使用servlet从索引访问不同的JSP文件

我正在尝试用TomCat建立一个简单的web应用程序,有一个servlet,它应该用指向两个不同JSP的两个按钮链接我的索引页面

它所做的事情是,当我点击这些按钮时,它不会更改页面,只会显示Servlet的名称“request”(抱歉,我不知道确切的术语)

另外,当我试图通过输入localhost:8080/reynumn直接访问时。jsp直接访问页面,它会停留在索引上,就好像它是那里唯一的页面一样,即使它看起来像是在尝试加载另一个页面

下面是Servlet和索引的代码

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
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 java.io.IOException;

@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
    private ServletContext sc = this.getServletContext();

    public void init(ServletConfig config) throws ServletException{
        super.init(config);
        sc = this.getServletContext();
    }

    public Servlet(){
        super();
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("operation");

        if (op.equalsIgnoreCase("cv")){
            this.versPersonne(request, response);
        }
        if (op.equalsIgnoreCase("reunion")){
            this.versReunion(request, response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }

    private void versReunion(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher rd = sc.getRequestDispatcher("/reunion.jsp");
        rd.forward(request, response);
    }

    private void versPersonne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher rd = sc.getRequestDispatcher("/cv.jsp");
        rd.forward(request, response);
    }


}

这是索引

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
  <title>$Title$</title>
</head>
<body>

<form action="Servlet?operation=cv" method="post">
  <br><input type=submit value="Page personne"/>
</form>

<form action="Servlet?operation=reunion" method="post">
  <br><input type=submit value="Page réunion"/>
</form>

</body>
</html>

以防万一,这里是网络。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>Servlet</servlet-name>
        <jsp-file>/index.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
        <servlet-name>Servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

我的JSP文件都在web文件夹中

我非常努力地寻找答案,尝试了许多不同的方法和配置,但似乎找不到答案

我确实认为问题在某种程度上在于servlet配置,但无法真正找到答案

更新:由于第一个答案,我删除了XML文件并更新了DoGet方法。 此时,web应用确实尝试切换页面,但它向我抛出了一个500错误异常,告诉我它未能实例化servlet。下面是日志:

javax.servlet.ServletException: Erreur à l'instantiation de la classe servlet [com.example.Servlet]
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:764)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1388)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:844)

根本原因是:

java.lang.NullPointerException
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:123)
com.example.Servlet.<init>(Servlet.java:15)
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:764)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1388)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:844)

这是项目结构:

https://imgur.com/a/YTpq4hD

提前感谢您的回答


共 (0) 个答案