Java servlet" HTTP状态500"使用自动页面刷新时

时间:2017-03-19 04:43:48

标签: java tomcat servlets

环境: Windows 8.1,Java 1.8.0,apache-tomcat-9.0.0.M17

servlet程序将创建一个显示滴答时间的servlet页面(必须使用自动页面刷新)。 HTML页面使用<select>使用户能够选择特定区域,而区域被发送到servlet文件。 但是发生了 HTTP Status 500 错误。 错误信息:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
    java.util.TimeZone.parseCustomTimeZone(TimeZone.java:783)
    java.util.TimeZone.getTimeZone(TimeZone.java:562)
    java.util.TimeZone.getTimeZone(TimeZone.java:516)
    AJprog2.doGet(AJprog2.java:20)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
note The full stack trace of the root cause is available in the Apache Tomcat/9.0.0.M17 logs.

Apache Tomcat/9.0.0.M17

以下是我的代码:

<html>  
    <body>
        <center>
        <form method = "post"
              action = "http://localhost:8080/examples/servlets/servlet/AJprog2">
            <p3> Select the timezone to display </p3> <br />    
            <select name = "sel_table" name = "sel_table">
                <option value = "IST">India</option>
                <option value = "GMT+8">China</option>
            </select>  
            <input type = submit value = "GO" />
        </form>
    </body>
</html>

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.Locale;
import javax.servlet.*;
import javax.servlet.http.*;

public class AJprog2 extends HttpServlet
{
    public void doGet(HttpServletRequest req, HttpServletResponse rsp)
    throws ServletException, IOException
    {
        final String tz = (String)req.getParameter("sel_table");
        rsp.setIntHeader("Refresh", 1);
        PrintWriter pan = rsp.getWriter();
        rsp.setContentType("text/html");
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss z");
        sdf.setTimeZone(TimeZone.getTimeZone(tz));
        String time_str = sdf.format(date);

        pan.println("<html>" +
                    "<body>" +
                    "<br />" +
                    time_str +
                    "</body></html>");
        // pan.close();
    }

    public void doPost(HttpServletRequest req, HttpServletResponse rsp)
    throws ServletException, IOException
    {
        doGet(req, rsp);
    }
}

问题可能涉及,当servlet页面刷新时,参数丢失。但我不知道如何解决它,任何人都可以帮忙检查一下吗?

1 个答案:

答案 0 :(得分:0)

当您刷新HTML时,它会丢失所有值,您不想要它,对吧?

您正试图从您的Servlet req.getParameter中获取此参数(&#34; sel_table&#34;);

发送帖子时在您的网页中

        <select name = "sel_table" name = "sel_table">

因此,如果您使用Javascript或jQuery刷新网站,则可以在刷新前始终设置参数,以便在需要时提供默认值;)

你也可以这样做:

String tz = (String)req.getParameter("sel_table");
 if (null == tz) tz = "IST"; //or a default value

有一次我在一本书中看到了,嘿容器,这里是我的请求参数,回答html,好像我关心你发送的东西;),换句话说它会忘记你所说的:)

希望它可以帮到你