-1

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

The servlet program is going to make a servlet page displaying the ticking time(auto page refresh must be used). The HTML page used <select> to make user able to choose the certain region, while the region is sent to servlet file. But HTTP Status 500 error occurs. The error information:

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

Here are my codes:

<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>

and

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);
    }
}

The problem may relate to, when the servlet page refreshed, the parameter lost. But I do not know how to solve it, could anyone help check?

fryingPan
  • 13
  • 4
  • 1
    Why do you have `name = "sel_table"` **twice** in that ` – Andreas Mar 19 '17 at 04:48
  • 1
    See [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/q/218384/5221149). You get that because `tz` is null. `tz` is null because the HTTP `Refresh` header will do a `GET`, not a `POST`, and hence your `sel_table` value is not submitted on the refresh. You solve it by explicitly giving the URL *with* the `sel_table` value as a query parameter as part of the `Refresh` header value, e.g. `rsp.setHeader("Refresh", "1; url=http://localhost:8080/examples/servlets/servlet/AJprog2?sel_table=" + tz);`. – Andreas Mar 19 '17 at 04:52
  • @Andreas sry, that is a mistake I made – fryingPan Mar 19 '17 at 05:03
  • @Andreas Thanks! Now it is working! – fryingPan Mar 19 '17 at 05:26

1 Answers1

0

When you refresh an HTML it loose all it's values, you don't want that, right?

You're trying to get this parameter from your Servlet req.getParameter("sel_table");

That is in your WebPage when you send the post

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

So if you're refreshing your website with Javascript or jQuery you can set always the parameter before refresh so it will give a default value if you want ;)

also you could do this:

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

Once i saw in a book, hey Container, here are my request parameters, THE html answered, like if I care what you send ;) , in other words it will forget what you said :)

Hope it can help you

Yussef
  • 610
  • 6
  • 11