浏览器显示来自Servlet的HTML代码

时间:2014-07-03 23:29:34

标签: java jsp servlets

已经看到很多类似的问题,但没有找到决定。

问题:浏览器显示servlet代码而不是执行此servlet。

这是index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <a href="servlets/LoginServlet.java"> Link </a>
    </body>
</html>

这是LoginServlet

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("Hello World");
        out.flush();
    }
}

的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_3_1.xsd"
     version="3.1">

<servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>LoginServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>

我将不胜感激。

4 个答案:

答案 0 :(得分:2)

 <a href="servlets/LoginServlet**.java**"> Link </a>

:P

您已在web.xml中映射/ LoginServlet,但指向链接中的源代码。

将您的链接映射到正确的URL,即您在web.xml中的URL:

<url-pattern>/LoginServlet</url-pattern>

所以链接必须是:

<a href="/LoginServlet" >Link </a>

因此,最终的URL将是:

的http:/本地主机:8080 / LoginServlet

答案 1 :(得分:1)

文件 LoginServlet.java 。 Chage doPost 指向 doGet

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.print("<html>\r\n");
        out.print("<head><title></title></head>\r\n");
        out.println("Hello World");
        out.print("</html>\r\n");
        out.flush();
    }
}



文件 index.jsp 。将超链接更改为&#34; / LoginServlet&#34;

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
<a href="/LoginServlet">Link</a>
</body>
</html>


文件web.xml :缺少结束标记:</web-app>。正确的文件:

<?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_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>
</web-app>

您应该将java代码放入 src 目录,如下所示:

enter image description here
(根据惯例和安全性,将java代码放在 web 目录中非常糟糕)

在接下来的其他情况下,您应该将Servlet放入包中

答案 2 :(得分:1)

您指定的网址格式为<url-pattern>/LoginServlet</url-pattern>,表示您的有效网址为<a href="/ProjectName/LoginServlet"/>

您指定<servlet-class>LoginServlet</servlet-class>时还有一件事意味着您没有任何包,因此请确保该类直接位于src文件夹下。

我建议创建一个包将servlet类放入其中并更改

<servlet-class>LoginServlet</servlet-class>

<servlet-class>packageName.LoginServlet</servlet-class>

答案 3 :(得分:1)

试试这个,你的道路给你一个问题

Html

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <a href="../LoginServlet"> Link </a>
    </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_3_1.xsd"
     version="3.1">

<servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>package_name.LoginServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>

别忘了重启服务器。