0

I've set up a dynamic web project that contains a jsp home page and a servlet HelloServlet java class.

The home page takes an input value from the jsp page and has a submit button to transfer the input value to the servlet class.

But when I click submit on the home page I get a HTTP Status 500 - Error instantiating servlet class HelloServlet

Does anyone know if I'm missing a step in setting this up? Or if there is a mistake in my web.xml descriptor?

Servlet class's doPost method is a follows:

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub


        // read form fields
        String fibNum = req.getParameter("fibNum");
        //print input from home page
        System.out.println("username: " + fibNum);

    }

This is how I have set up the mappings in the web.xml descriptor:

  <servlet>
  <servlet-name>hello</servlet-name>
  <servlet-class>HelloServlet</servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>hello</servlet-name>
  <url-pattern>/say_hello/*</url-pattern>
</servlet-mapping>
Brian Var
  • 6,029
  • 25
  • 114
  • 212

1 Answers1

2

You need to specify the package along with class in web.xml:

<servlet>
  <servlet-name>hello</servlet-name>
  <servlet-class>ie.gmit.HelloServlet</servlet-class>
</servlet>

Also you can just get rid of the * here:

<servlet-mapping>
  <servlet-name>hello</servlet-name>
  <url-pattern>/say_hello</url-pattern>
</servlet-mapping>

Also you are handling the post method in your servlet but sending a get request via the form. You can either change form to method="post" or put this in your servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        // TODO Auto-generated method stub
        doPost(request, response);
}
brso05
  • 13,142
  • 2
  • 21
  • 40
  • The above solution worked,great! I'm wondering now how after the value has been passed over,how could I call a another page `result` from the java servlet class `HelloServlet`? Basically it would be the reverse operation as it would be passing the value from the servlet back to a jsp page. Any help is appreciated. – Brian Var Dec 09 '14 at 18:52
  • 1
    @BrianJ you can use `response.sendRedirect("url for jsp page");` to redirect to the jsp page. If you want to just send a couple parameters to the jsp you can put them in the url at the end url?name=value&nam1=value1 or if you want you can store an object in session and retrieve it in the jsp page. To get the url params use `request.getParameter("name")` to get the session you can do `(WhateverObject)session.getAttribute("name")`; To store in the session in servlet do `request.getSession().setAttribute(yourObject)` – brso05 Dec 09 '14 at 19:22
  • Okay I'm assuming I add the same mappings in the web.xml for the `result` page then? Like this: ` result ie.gmit.HelloServlet result /say_result ` – Brian Var Dec 09 '14 at 20:30
  • @BrianJ Not if you are redirecting to jsp it is automatically taken care of. You don't need another servlet to redirect to a jsp just go straight to the jsp. Are you trying to go from HelloServlet->result.jsp? – brso05 Dec 09 '14 at 20:39
  • Yes, so the input value is passed to the servlet, then I want to pass it back to a second jsp page, `result`. I'm thinking then I could just do the following according to your above comment`in the HelloServlet class. `response.sendRedirect("result/fibNum");` I don't think this is the correct way to specify the url though? – Brian Var Dec 09 '14 at 20:59
  • 1
    Depending on where your `result.jsp` file is this example is for if it is in the root: `response.sendRedirect(("result.jsp?fibNum=" + fibNum);` to retrieve the value in your jsp you can use scriplet tags and do this: `<% String fibNum = request.getParameter("fibNum"); %>` – brso05 Dec 09 '14 at 21:29
  • The result.jsp page is in the same place as the home.jsp page, the `WEB-INF` folder. Is this considered the root? In the welcome file of the web.xml descriptor the `result` file is second after `home`. – Brian Var Dec 09 '14 at 21:36
  • @BrianJ the `result.jsp` and the `home.jsp` should probably be at the same level as `WEB-INF` folder not inside but at same level... – brso05 Dec 09 '14 at 21:38