Reputation: 847
I am trying a simple login servlet with http doPost().
The Login.html is:
<form action="P3" method="post">
Username :-
<input type="text" name="usnm"><p>
Password :-
<input type="password" name="pswd"><p>
<input type="submit" value="Login">
</form>
My servlet code is: Server.java:
public void doPost(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
try
{
res.setContentType("text/html");
String un=req.getParameter("usnm");
String pw=req.getParameter("pswd");
if(un.equals("abc") && pw.equals("123"))
res.sendRedirect("welcome.html");
else
res.sendRedirect("nologin.html");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
If login is successful then welcome.html is to be shown or else nologin.html.
I added this to my web.xml:
<servlet>
<servlet-name>z</servlet-name>
<servlet-class>Server</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>z</servlet-name>
<url-pattern>/P3</url-pattern>
</servlet-mapping>
When I try to execute the Login.html after 'Login' click, I get error: can't find the file at /C:/Users/OWNER/Desktop/apache-tomcat-7.0.37/webapps/vaishnavee/P3.
Please suggest some solution to make this work.
Upvotes: 1
Views: 721
Reputation: 34608
You are probably displaying the form directly from your hard disk, rather than the tomcat server. You can probably see this in the address field of your browser: file://C:/Users/OWNER/Desktop/apache-tomcat-7.0.37/webapps/vaishnavee/Login.html
instead of http://..../Login.html
.
The action of the form is relative to the place where the form itself came from. So if the form itself came directly from drive C:
instead of tomcat, then the browser will calculate the full URL of the action based on the same URL you see in your address field, but with Login.html
replaced with P3
. Of course, you don't have a file by that name so the form cannot be passed to it.
You should either:
http://localhost/Login.html
but you have to use whatever URL was configured for your server)P3
.The first way is the preferred way, as serving the form directly from your C:
drive is not going to work when you deploy the application or hand it out.
Upvotes: 1