Servlets与IntelliJ

huangapple go评论70阅读模式
英文:

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 = &quot;Test&quot;)
public class Test extends HttpServlet {
    protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType (&quot;text/html&quot;);
        response.setCharacterEncoding (&quot;UTF-8&quot;);

        PrintWriter out = response.getWriter();
        out.println(&quot;&lt;!DOCTYPE html&gt;&quot;);
        out.println(&quot;&lt;html&gt;&quot;);
        out.println(&quot;&lt;head&gt;&quot;);
        out.println(&quot;&lt;meta charset=\&quot;utf-8\&quot; /&gt;&quot;);
        out.println(&quot;&lt;title&gt;Test&lt;/title&gt;&quot;);
        out.println(&quot;&lt;/head&gt;&quot;);
        out.println(&quot;&lt;body&gt;&quot;);
        out.println(&quot;&lt;p&gt;Bonjour !&lt;/p&gt;&quot;);
        out.println(&quot;&lt;/body&gt;&quot;);
        out.println(&quot;&lt;/html&gt;&quot;);
    }

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

    }
}

Et le web.xml :


    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;web-app 
            xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;
             xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
             xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot;
             version=&quot;4.0&quot;&gt;
        &lt;servlet&gt;
            &lt;servlet-name&gt;Test&lt;/servlet-name&gt;
            &lt;servlet-class&gt;com.octest.servlets.Test&lt;/servlet-class&gt;
        &lt;/servlet&gt;
        
        &lt;servlet-mapping&gt;
            &lt;servlet-name&gt;Test&lt;/servlet-name&gt;
            &lt;url-pattern&gt;/app&lt;/url-pattern&gt;
        &lt;/servlet-mapping&gt;
    &lt;/web-app&gt;

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.

huangapple
  • 本文由 发表于 2020年7月25日 18:00:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63086961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定