英文:
Servlets & IntelliJ
问题
我在Intellij下创建了一个测试项目,使用Tomcat来启动Java EE。
我的服务器启动得很好,没有任何问题。
我创建了一个包含HTML代码的Servlet,还有doPost和doGet方法。
当我想要运行这个Servlet时,服务器启动正常,但最终页面是空白的。
有什么想法吗?
代码:
package com.octest.servlets;
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.io.PrintWriter;
@WebServlet(name = "Test")
public class Test extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"utf-8\" />");
out.println("<title>Test</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>Bonjour !</p>");
out.println("</body>");
out.println("</html>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
还有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>Test</servlet-name>
<servlet-class>com.octest.servlets.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/app</url-pattern>
</servlet-mapping>
</web-app>
谢谢
英文:
I created a test project under Intellij to start Java EE with Tomcat.
My server starts well, no worries aside.
I created a Servlet which contains my HTML code, as well as doPost and getPost.
When I want to run the servlet, the server launches fine but I end up with a blank page.
An idea ?
Code :
package com.octest.servlets;
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.io.PrintWriter;
@WebServlet(name = "Test")
public class Test extends HttpServlet {
protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType ("text/html");
response.setCharacterEncoding ("UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"utf-8\" />");
out.println("<title>Test</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>Bonjour !</p>");
out.println("</body>");
out.println("</html>");
}
protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
Et le 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>Test</servlet-name>
<servlet-class>com.octest.servlets.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/app</url-pattern>
</servlet-mapping>
</web-app>
Thanks
答案1
得分: 1
浏览器将发起GET请求,但你的HTML页面在doPost
方法中。你需要将此页面放在doGet
方法内部。
英文:
The browser will make a GET request but your HTML page is in doPost
method. You need to keep this page inside doGet
method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论