Servlets - 发生奇怪的现象,简单的 Servlet 被下载而不是执行?
Servlets - Strange phenomenon occuring, Simple Servlet getting downloaded instead of execution?
我设计了一个简单的 servlet
,但它没有工作 它没有执行,而是通过浏览器下载 window。
当我执行代码时,HTML 页面运行正常,但在我提交表单数据后
servlet 在没有执行 servlet 的情况下如图所示被下载..
我尝试了很多次修改代码并进行了搜索我在任何地方都没有找到这个,所以感谢您的帮助
请看我附上的截图:
图片1(http://imgur.com/pW4mgmK)
图片2(http://imgur.com/p4K5Rrs)
//CLASS file
package com.pkg;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//@WebServlet("/SltClass")
public class SltClass extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("HTML/text");
String userNameVar = request.getParameter("userName");
String passWordVar = request.getParameter("passWord");
PrintWriter out = response.getWriter();
out.println("hello user :" +userNameVar);
}}
HTML
</head>
<body>
<div>TODO write content</div>
<form action="Welcome" method="post">
<input type="text" name="userName" />
<input type="text" name="passWord" />
<input type="submit" value="Submit it" />
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Sname</servlet-name>
<servlet-class>com.pkg.SltClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Sname</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
你能看看用 html 切换文本是否有什么不同 - 像这样:
response.setContentType("text/html");
我设计了一个简单的 servlet
,但它没有工作 它没有执行,而是通过浏览器下载 window。
当我执行代码时,HTML 页面运行正常,但在我提交表单数据后 servlet 在没有执行 servlet 的情况下如图所示被下载..
我尝试了很多次修改代码并进行了搜索我在任何地方都没有找到这个,所以感谢您的帮助
请看我附上的截图:
图片1(http://imgur.com/pW4mgmK) 图片2(http://imgur.com/p4K5Rrs)
//CLASS file
package com.pkg;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//@WebServlet("/SltClass")
public class SltClass extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("HTML/text");
String userNameVar = request.getParameter("userName");
String passWordVar = request.getParameter("passWord");
PrintWriter out = response.getWriter();
out.println("hello user :" +userNameVar);
}}
HTML
</head>
<body>
<div>TODO write content</div>
<form action="Welcome" method="post">
<input type="text" name="userName" />
<input type="text" name="passWord" />
<input type="submit" value="Submit it" />
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Sname</servlet-name>
<servlet-class>com.pkg.SltClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Sname</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
你能看看用 html 切换文本是否有什么不同 - 像这样:
response.setContentType("text/html");