我正在尝试使用TomCat设置一个简单的Web应用程序,其中有一个servlet应该将我的索引页面与指向2个不同JSP的2个按钮链接起来。
它的作用是,当我单击这些按钮时,它不会更改页面,它仅放置Servlet的名称“ request”(对不起,您实际上不知道确切的用语)。
另外,当我尝试通过键入例如localhost:8080 / reunion.jsp直接访问页面来直接访问时,即使看起来好像正在尝试加载另一个页面,它仍保留在索引上,就好像它是那里唯一的页面。 / p>
这是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>
以防万一,这是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>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)
这是项目结构:
预先感谢您的回答!
答案 0 :(得分:0)
调用方法doPost(request,response);如下所示,在doGet方法中可以解决您的问题。
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
还在Index.jsp中,您将表单操作指定为"Servlet?operation=reunion"
将其更改为"/Servlet?operation=reunion"
类似地将"Servlet?operation=cv"
更改为"/Servlet?operation=cv"
答案 1 :(得分:0)
您的web.xml
文件是罪魁祸首。
您想在/Servlet
批注中将servlet用作WebServlet
,但同时xml文件将index.jsp
映射到任何URL 与/*
。
您已经声明了带有WebServlet
批注的映射,并且由于index.jsp
是默认页面,因此只需删除映射眉头web.xml
并将其剥离为:
<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">
</web-app>
或者甚至更简单地删除web.xml文件,因为它不再包含任何内容。
您的servlet代码中还有另一个错误:您尝试在创建时初始化ServletContext,但不应这样做。应该是:
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private ServletContext sc; // delay initialization to init
public void init(ServletConfig config) throws ServletException{
super.init(config);
sc = this.getServletContext();
}
但是实际上您没有理由存储对ServletContext的引用。仅当可以从RequestDispatcher
获取它时,才使用它来获取Request
:
private void versReunion(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("/reunion.jsp");
rd.forward(request, response);
}
仅此而已。在versPersonne
中执行相同的操作,您可以删除引用sc
的任何行...