servlets and jsp doubt

S

sangeeta chowdhary

hi,
I have written a servlet

import music.business.User;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import music.data.*;

public class RegisterUserServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException,

ServletException {
doPost(request, response);
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
HttpSession session = request.getSession();

String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress = request.getParameter("emailAddress");

User user = new User();
user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmailAddress(emailAddress);
try
{
if (UserDB.emailExists(emailAddress))
UserDB.update(user);
else
UserDB.insert(user);
} catch(Exception e) {}
session.setAttribute("user", user);

Cookie emailCookie = new Cookie("emailCookie", emailAddress);
emailCookie.setMaxAge(60*60*24*365*2);
emailCookie.setPath("/");
response.addCookie(emailCookie);

String url = "/catalog/writeDownload";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}

}


my jsp code is calling this servlet


<jsp:include page="/includes/header.html" />
<jsp:include page="/includes/column_left_all.jsp" />

<!-- start the middle column -->

<td>

<script language="JavaScript">
function validate(form) {
if (form.firstName.value=="") {
alert("Please fill in your first name");
form.firstName.focus();
}
else if (form.lastName.value=="") {
alert("Please fill in your last name");
form.lastName.focus();
}
else if (form.emailAddress.value=="") {
alert("Please fill in your email address");
form.emailAddress.focus();
}
else {
form.submit();
}
}
</script>

<h1>Download registration</h1>

<p>Before you can download and listen to these sound files,
you must register with us by entering your name and email
address below.</p>

<!-- Import the core JSTL library -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!-- Use the JSTL url tag to encode the URL -->
<form action="<c:url value='/catalog/registerUser'/>"
method="post">
<table cellpadding="5" border="0">
<tr>
<td align="right"><p>First name:</td>
<td><input type="text" name="firstName"></td>
</tr>
<tr>
<td align="right"><p>Last name:</td>
<td><input type="text" name="lastName"></td>
</tr>
<tr>
<td align="right"><p>Email address:</td>
<td><input type="text" name="emailAddress"></td>
</tr>
<tr>
<td></td>
<td><input type="button" value="Submit"
onClick="validate(this.form)"></td>
</tr>
</table>
</form>

</td>

<!-- end the middle column -->

<jsp:include page="/includes/column_right_buttons.jsp" />
<jsp:include page="/includes/footer.jsp" />


when i run my servlet,browser display this message-

HTTP Status 405 - HTTP method GET is not supported by this URL

i have given proper url to this servlet through web.xml also.

kindly help me to provide solution for this problem.
Thank you.
 
L

Lew

sangeeta said:
I have written a servlet
...
        <td><input type="button" value="Submit"
                   onClick="validate(this.form)"></td>
      </tr>
    </table>
  </form>
...
when i [sic] run my servlet,browser display this message-

HTTP Status 405 - HTTP method GET is not supported by this URL

i [sic] have given proper url to this servlet through web.xml also.

What happens if you eliminate the 'onClick' from the input button?

Also, I am pretty sure you should not use "<c:url>" in the form
'action' attribute, but just the servlet reference:

<form action="/catalog/registerUser" method="post">

I realize you're trying to prevent issues if cookies are disabled, but
I don't think that consideration applies to the 'action' attribute.
You can tell that I'm not certain about this, so try it and report
what happens.

There are several petty issues with your code as well, not relevant to
your question nor likely to cause you serious trouble in this
particular case, but they represent bad habits such as failure to
enclose 'if' clauses in curly braces.

Really good example you posted, btw. Nicely complete.
 
S

sangeeta chowdhary

sangeeta said:
I have written a servlet
...
        <td><input type="button" value="Submit"
                   onClick="validate(this.form)"></td>
      </tr>
    </table>
  </form>
...
when i [sic] run my servlet,browser display this message-
HTTP Status 405 - HTTP method GET is not supported by this URL
i [sic] have given proper url to this servlet through web.xml also.

What happens if you eliminate the 'onClick' from the input button?

Also, I am pretty sure you should not use "<c:url>" in the form
'action' attribute, but just the servlet reference:

 <form action="/catalog/registerUser" method="post">

I realize you're trying to prevent issues if cookies are disabled, but
I don't think that consideration applies to the 'action' attribute.
You can tell that I'm not certain about this, so try it and report
what happens.

There are several petty issues with your code as well, not relevant to
your question nor likely to cause you serious trouble in this
particular case, but they represent bad habits such as failure to
enclose 'if' clauses in curly braces.

Really good example you posted, btw.  Nicely complete.

Thanks for your reply sir.
I have tried using -
<form action="/catalog/registerUser" method="post">

but it is still not working.
If i remove onClick button then it will not validate the form.
 
L

Lew

sangeeta chowdhary quoted the sig:
Please do not quote sigs.

sangeeta said:
If i [sic] remove onClick button then it will not validate the form.

Not on the client side, no, it won't, but that doesn't matter a whit.
Sometimes when you are diagnosing the problem you have to try things
that you know won't stay in the final version, but are necessary to
understand the situation.

I ask again, what happens if you eliminate the 'onClick' from the
input button? I mean, with respect to the bug you're hunting, of
course. I wonder (because I do not know) if somehow the JS is forcing
a GET instead of a POST. OTOH, you handle GET, so I'm mystified.

I really don't see in your code how that error 405 can happen, so I'm
taking shots in the dark.
 
J

John B. Matthews

Lew said:
sangeeta chowdhary quoted the sig:
Please do not quote sigs.

sangeeta said:
If i [sic] remove onClick button then it will not validate the form.

Not on the client side, no, it won't, but that doesn't matter a whit.
Sometimes when you are diagnosing the problem you have to try things
that you know won't stay in the final version, but are necessary to
understand the situation.

I ask again, what happens if you eliminate the 'onClick' from the
input button? I mean, with respect to the bug you're hunting, of
course. I wonder (because I do not know) if somehow the JS is
forcing a GET instead of a POST. OTOH, you handle GET, so I'm
mystified.

If doGet() was a late addition, I'd wonder if the servlet container
needs to be restarted or the context reloaded. Using Tomcat, I've become
accustomed to the convenience of Manager App:
 
S

sangeeta chowdhary

sangeeta chowdhary quoted the sig:

Please do not quote sigs.

sangeeta said:
If i [sic] remove onClick button then it will not validate the form.

Not on the client side, no, it won't, but that doesn't matter a whit.
Sometimes when you are diagnosing the problem you have to try things
that you know won't stay in the final version, but are necessary to
understand the situation.

I ask again, what happens if you eliminate the 'onClick' from the
input button?  I mean, with respect to the bug you're hunting, of
course.  I wonder (because I do not know) if somehow the JS is forcing
a GET instead of a POST.  OTOH, you handle GET, so I'm mystified.

I really don't see in your code how that error 405 can happen, so I'm
taking shots in the dark.

Good Morning sir,

Thanks for your reply.

i have tried what you have asked me for,but now its giving error-

Address not found:

You tried to access the address http://localhost:8080/musicStore/catalog/registerUser,
which is currently unavailable. Please make sure that the Web address
(URL) is correctly spelled and punctuated, then try reloading the
page.
Make sure your Internet connection is active and check whether other
applications that rely on the same connection are working.
Check that the setup of any Internet security software is correct and
does not interfere with ordinary Web browsing.
If you are behind a firewall on a Local Area Network and think this
may be causing problems, talk to your systems administrator.
Try pressing the F12 key on your keyboard and disabling proxy servers,
unless you know that you are required to use a proxy to connect to the
Internet. Reload the page.
Need help?
Open the Opera Help.
Go to Opera's online support desk.
 
S

sangeeta chowdhary

 Lew said:
sangeeta chowdhary quoted the sig:
Please do not quote sigs.
sangeeta said:
If i [sic] remove onClick button then it will not validate the form.
Not on the client side, no, it won't, but that doesn't matter a whit.
Sometimes when you are diagnosing the problem you have to try things
that you know won't stay in the final version, but are necessary to
understand the situation.
I ask again, what happens if you eliminate the 'onClick' from the
input button?  I mean, with respect to the bug you're hunting, of
course.  I wonder (because I do not know) if somehow the JS is
forcing a GET instead of a POST.  OTOH, you handle GET, so I'm
mystified.

If doGet() was a late addition, I'd wonder if the servlet container
needs to be restarted or the context reloaded. Using Tomcat, I've become
accustomed to the convenience of Manager App:

I really don't see in your code how that error 405 can happen, so I'm
taking shots in the dark.

good morning sir,
thanks for your reply.

i have tried what you have asked to do,now i am getting this error-

Address not found:

You tried to access the address http://localhost:8080/musicStore/catalog/registerUser,
which is currently unavailable. Please make sure that the Web address
(URL) is correctly spelled and punctuated, then try reloading the
page.
Make sure your Internet connection is active and check whether other
applications that rely on the same connection are working.
Check that the setup of any Internet security software is correct and
does not interfere with ordinary Web browsing.
If you are behind a firewall on a Local Area Network and think this
may be causing problems, talk to your systems administrator.
Try pressing the F12 key on your keyboard and disabling proxy servers,
unless you know that you are required to use a proxy to connect to the
Internet. Reload the page.
Need help?
Open the Opera Help.
Go to Opera's online support desk.
 
L

Lew

sangeeta said:
i [sic] have tried what you have asked to do,now i [sic] am getting this error-

Address not found:

You tried to access the addresshttp://localhost:8080/musicStore/catalog/registerUser,
which is currently unavailable. Please make sure that the Web address
(URL) is correctly spelled and punctuated, then try reloading the
page.

That says the app server isn't running, or else not on port 8080, or
else the "musicStore" app is not running.
 
J

John B. Matthews

Arne Vajhøj said:
Me too.

Especially if called from build.xml it is very convenient.

Thanks for calling attention to this feature. I've habitually used the
manager from the browser, and I'd overlooked the section "Executing
Manager Commands With Ant."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top