views:

84

answers:

1

I have this code on my JSP page

<form action="LoginServlet" method="get">
    <input type="text" name="username" /> 
    <input type="text" name="password" /> 
    <input type="submit" value="Submit" />
</form>

and when I press Submit I get:

The page cannot be displayed The page you are looking for is currently unavailable. The Web site might be experiencing technical difficulties, or you may need to adjust your browser settings.

and LoginServlet.doGet method doesn't get called. But then when I press Enter again (in address bar) my doGet method gets called.

What is wrong? I am using JEE eclipse and Tomcat

A: 

What's in your web.xml file?

You WEB-INF/web.xml file needs to associate the LoginServlet specified in the JSP with the Java class LoginServlet. (For clarity I have changed the name of values.)

So if in your JSP you have

<form action="jspAction" method="get"> 
    <input type="submit" value="Submit" /> 
</form> 

and your Java class is

package com.me

import javax.servlet.*;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet {
  public void goGet(HttpServletRequest request, 
     HttpServletResponse response)
        throws ServletException, IOException
  {
     //your code
  }
}

Your web.xml file would have

<web-app>
    <!-- Standard Action Servlet Configuration -->
    <servlet>
        <servlet-name>myServletName</servlet-name>
        <servlet-class>com.me.LoginAction</servlet-class>
    </servlet>
    <!-- Standard Action Servlet Mapping -->
    <servlet-mapping>
        <servlet-name>myServletName</servlet-name>
        <url-pattern>jspAction</url-pattern>
    </servlet-mapping>
</web-app>

So the web.xml file will associate myServletName with the servlet class com.me.LoginAction. Then any requests to http://localhost:8080/myApp/jspAction will be directed to myServletName and eventually to com.me.LoginAction.

Prof