Search code examples
javahtmljspservletsjsp-tags

Why is this simple jsp program is NOT running?


As we know every jsp program there is a servlet behind the jsp page. I have used a jsp page to make a form (its a very small form), and in the same jsp i used scriptlet tags and made a way to get the inserted form data, and display it using out.print(). but the problem is it when i run it, the form is displayed., but when i submit is, it doesn't recognize the servlet page (error coming as "The requested resource is not available"). i will put the code below., please help me friends to solve this problem. thank you. i did this in netbeans. jsp page name is- "hello.jsp" the servlet page name behind the jsp page is: "hello_jsp.java".

<html> 
<head><title>IF...ELSE Example</title></head> 
<body>  
<form action="hello_jsp" method="post">
  <input type="text" name="y"/>
  <input type="submit" value="submit"/>

<% 
  if(request.getParameter("y")!=null) { 
    String s = request.getParameter("y"); 
    if(s.equals("hello")){
    out.print("welcome"+s);
  }else{
    out.print("not welcome");
  }}
%> 

</form>
</body> 
</html> 

Solution

  • My guess is that you need to change

    <form action="hello_jsp" method="post">
    

    to

    <form action="hello.jsp" method="post">
    <!--               ^---- change is here -->
    

    The externally-accesible resource is the jsp, not the servlet. (By default, I'm sure some config-fu could change that.)

    Or, of course, if the page is supposed to submit to itself, don't include action at all. The default is to submit to the current page.

    <form method="post">