I am trying to get info from the html form from my servlet. My problem is I cant get to wanted method by click() method on my servlet. my servlet is on package login and its name loginServlet. If you can tell my what wrong with my code :
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n" +
"<HTML>\n" +
"<HEAD><TITLE>login</TITLE></HEAD>\n" +
"<BODY>\n" +
"<FORM ACTION=login/loginServlet METHOD=click>"+
"<table border ='1'>" +
"<tr>" +
"<th colspan='2'>Register Account Information</th>" +
"</tr>" +
"<tr>" +
"<td>Requested Username:</td>" +
"<td><input type='text' name='username' /></td>" +
"</tr>" +
"<tr>" +
"<td>Password:</td>" +
"<td><input type='password' name='password' /></td>" +
"</tr>" +
"<tr>" +
"<th colspan='2'><input type='Submit' value='Submit'>"+
"</tr>" +
"</table>"+
"</FORM></BODY></HTML>");
}
public void click(){
int i=0;
for (i=0;i<1000;i++){
}
thanks开发者_如何学C a lot!
As a start, never ever use servlets to spit out static html like that. Never.
After that's been fixed, you need to learn about how web servers work: a servlet, or JSP, produces a chunk of html that gets sent to the browser. The browser will render it (show on screen) and look for specific instructions. A <form>
basically says: let user fill out whatever fields we have in this form and then call the page/service indicated by the action=
attribute whereby all form fields are given back as variables.
You probably need to study a bit about web programming, a good free resource for Java web tech is the Java EE tutorial. You'll see that JSP technology is being phased out in favor of JSF (that's why I point to version 5 of the tutorial) but I'd definitely suggest you start with JSP.
you are way off...
the attribute METHOD is for a GET (method called is the servlet's doGet()) or POST request (method called is the servlet's doPost()), not for any specific servlet's method.
So, accordingly, write your code in the respective method.
You're misunderstanding the <form>
tag.
You need to specify a URL that will end up executing your method.
The HTML method
attribute specifies the HTTP request verb and can be GET
or POST
.
精彩评论