By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 3,407 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

Urgent:Can anyone tell me where i'm worng in this servlet program

P: 54
Hi All,

I have the following code for form and servlet. when the form is submitted the servlet will print the values passed by the form. i'm not getting the servlet o/p when i submit the form.here is my code:(simple code just for testing i did this program)

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <form name="login" method="POST" action="/servlet/LoginServlet">
  9. UserName<input type="text" name="username"/>
  10. Password<input type="password" name="pwd"/>
  11. <input type="button" value="Submit"/>
  12. </form>
  13. </body>
  14. </html>
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. import java.io.IOException;
  22. import java.io.PrintWriter;
  23.  
  24. import javax.servlet.ServletException;
  25. import javax.servlet.http.HttpServletRequest;
  26. import javax.servlet.http.HttpServletResponse;
  27.  
  28. /**
  29.  * Servlet implementation class for Servlet: LoginServlet
  30.  *
  31.  */
  32.  public class LoginServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
  33.     /* (non-Java-doc)
  34.      * @see javax.servlet.http.HttpServlet#HttpServlet()
  35.      */
  36.     public LoginServlet() {
  37.         super();
  38.     }       
  39.  
  40.     /* (non-Java-doc)
  41.      * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  42.      */
  43.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  44.         // TODO Auto-generated method stub
  45.         System.out.println("in the login method");
  46.         PrintWriter out = response.getWriter();
  47.         out.println("Came to the login page");
  48.         String name = request.getParameter("username");
  49.         String password = request.getParameter("pwd");
  50.         out.println("name is:" +name);
  51.         out.println("pwd is:" +password);
  52.         System.out.println("name is:" +name);
  53.         System.out.println("password is:" +password);
  54.  
  55.     }      
  56.  
  57.     /* (non-Java-doc)
  58.      * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  59.      */
  60.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  61.         // TODO Auto-generated method stub
  62.     }                 
  63. }
  64.  
  65.  
I'm accessing the user.html(which has form) using http://localhost:8080/User/user.html. where User is the folder name which contains the html and servlet program.

The url pattern defined as <url-pattern>/LoginServlet</url-pattern> in web.xml for the servlet
Sep 14 '07 #1
Share this Question
Share on Google+
14 Replies


madhoriya22
100+
P: 252
Hi,

What these two lines ...
Expand|Select|Wrap|Line Numbers
  1. System.out.println("name is:" +name);
  2. System.out.println("password is:" +password);
  3.  
printing on the console ?
Sep 14 '07 #2

P: 54
Hi,

What these two lines ...
Expand|Select|Wrap|Line Numbers
  1. System.out.println("name is:" +name);
  2. System.out.println("password is:" +password);
  3.  
printing on the console ?
yes i'm printing in the console also
Sep 14 '07 #3

madhoriya22
100+
P: 252
yes i'm printing in the console also
Hi,
Then what is printing on the console ?
Sep 14 '07 #4

P: 41
yes i'm printing in the console also
Hi,

One concern u r extending the HttpServlet class so u need not to implement Servlet interface again as because HttpServlet class is implementing the Servlet interface.


Regards,
Sateesh.
Sep 14 '07 #5

10K+
P: 13,264
yes i'm printing in the console also
Which output are you not getting then?
Sep 14 '07 #6

P: 54
Which output are you not getting then?

when i submitted the form i'm not able to see the name and password values neither in the console nor in the html page
Sep 14 '07 #7

Nepomuk
Expert 2.5K+
P: 3,112
when i submitted the form i'm not able to see the name and password values neither in the console nor in the html page
So the output of
Expand|Select|Wrap|Line Numbers
  1. System.out.println("name is:" +name);
  2. System.out.println("password is:" +password);
  3.  
is
Expand|Select|Wrap|Line Numbers
  1. name is:
  2. password is:
  3.  
(Without adding the values for name and password.) Is that correct? Or do you get no output at all?

Greetings,
Nepomuk
Sep 14 '07 #8

10K+
P: 13,264
when i submitted the form i'm not able to see the name and password values neither in the console nor in the html page
Your form submit method is "post" but you have overriden the doGet method.

Note that if you override the service method instead, then your code will be executed whether the form uses get or post.
Sep 14 '07 #9

P: 54
So the output of
Expand|Select|Wrap|Line Numbers
  1. System.out.println("name is:" +name);
  2. System.out.println("password is:" +password);
  3.  
is
Expand|Select|Wrap|Line Numbers
  1. name is:
  2. password is:
  3.  
(Without adding the values for name and password.) Is that correct? Or do you get no output at all?

Greetings,
Nepomuk
i'm not getting any output
Sep 14 '07 #10

madhoriya22
100+
P: 252
i'm not getting any output
hi,
see reply #9 and try what r0 is saying
Sep 14 '07 #11

P: 54
hi,
see reply #9 and try what r0 is saying

Tried that way also but not able to get the output
Sep 14 '07 #12

10K+
P: 13,264
Tried that way also but not able to get the output
Which way? Can you post what you've done.
Sep 14 '07 #13

P: 54
Which way? Can you post what you've done.
Sorry as i was busy i have'nt checked your reply.

in the reply #9 it's mentioned like "Your form submit method is "post" but you have overriden the doGet method.
Note that if you override the service method instead, then your code will be executed whether the form uses get or post." by you. So i have modified my code and checked but even then when i submit the form with values they are not getting displayed in the console nor in the html page.
Sep 19 '07 #14

10K+
P: 13,264
Sorry as i was busy i have'nt checked your reply.

in the reply #9 it's mentioned like "Your form submit method is "post" but you have overriden the doGet method.
Note that if you override the service method instead, then your code will be executed whether the form uses get or post." by you. So i have modified my code and checked but even then when i submit the form with values they are not getting displayed in the console nor in the html page.
I also asked you to post the changes you've made. Did you rebuild the application after those changes?
Also make sure that your servlet path is correct.
Sep 19 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.