我基本上已经开始大脑锁定了,在用户点击提交后,它转到一个空白页面甚至没有到达servlet。我放了一个System.out.println
来测试它是否到达servlet但它没有。
这是web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>servlet03</servlet-name>
<servlet-class>servlets.Week01_03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet03</servlet-name>
<url-pattern>/week01_03servlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
这是servlet
package servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Week01_03 extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("Hello");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String userString = request.getParameter("userString");
out.close();
char[] vowel = {'a', 'e', 'i', 'o', 'u'};
int[] count = {0, 0, 0, 0, 0};
for (int i = 0; i < userString.length(); i++)
{
switch (userString.charAt(i))
{
case 'a':
case 'A':
count[0]++;
break;
case 'e':
case 'E':
count[1]++;
break;
case 'i':
case 'I':
count[2]++;
break;
case 'o':
case 'O':
count[3]++;
break;
case 'u':
case 'U':
count[4]++;
break;
default:
break;
}
}
try
{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Week01_03</title>");
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"Week01_02.css\">");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Week 01, Exercise 03</h1>");
out.println("<p class=\"inline\">You typed: '<pre class=\"inline\">" +
userString + "</pre>'</p>");
out.println("<h3>Vowel usage:</h3>");
out.println("<table>");
for (int i = 0; i < vowel.length; i++)
{
out.println("<tr" + (i%2==0 ? " class=\"shaded\"" : "") + ">" +
"<td>'" + vowel[i] +
"'</td><td>" + count[i] +
"</td></tr>");
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
finally
{
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws javax.servlet.ServletException
* @throws java.io.IOException
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws javax.servlet.ServletException
* @throws java.io.IOException
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*/
public String getServletInfo()
{
return "Short description";
}// </editor-fold>
}
这是html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Week 01, Exercise 03</title>
<link rel="stylesheet" type="text/css" href="Week01_03.css">
</head>
<body>
<h1>Week 01, Exercise 03</h1>
<form action="week01_03servlet" method="post">
<p>Type some text in the box:
<input type="text" name="userString"></p>
<p>and then click the button,
and I'll tell you the frequency of vowel usage:
<input type="submit" value="Submit"></p>
</form>
</body>
</html>
感谢您的回复。
答案 0 :(得分:1)
这个servlet不起作用的原因是因为out.close()
方法开头的processRequest()
指令。
System.out.println("Hello");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String userString = request.getParameter("userString");
**out.close();**
它过早地关闭了用于向客户端发送响应的PrintWriter。因此,客户端仅获得HTTP header content-type: "text/html;charset=UTF-8"
的响应,但空体。
答案 1 :(得分:0)
我同意Philippe G的观点,这个问题很可能过早了。关闭()
您之前已经向控制台打印了一条消息,但可能看不到它。由于日志配置的原因,输出可能不是您所期望的,并且System.out可能会被缓冲,直到一段时间后才会被写出。
您通常应该将此类system.out.println切换为使用System.err.println,并确保输出不会从某个地方发送到日志文件,或者被容器完全丢弃
答案 2 :(得分:0)
在doPost末尾使用重定向。
response.sendRedirect("/yourpage");