Good Day,
I have just configured the tomcat and using the java servlet pages. I'm new with this and unable to hit the index page successfully but if I directly tried to hit the form action and passed the defined param then I could see the results. Please guide me if I'm missing something.
JSP - Code
<div align="center" style="margin-top: 50px;">
<form action="CrunchifyServlet">
Please enter your Username: <input type="text" name="username" size="20px"> <br>
Please enter your Password: <input type="text" name="password" size="20px"> <br><br>
Please enter your Age: <input type="text" name="age" size="20px"> <br><br>
<input type="submit" value="submit">
</form>
</div>
Java - Code
public class HelloCrunchify extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// reading the user input
String username = request.getParameter("username");
String password = request.getParameter("password");
String age = request.getParameter("age");
PrintWriter out = response.getWriter();
out.println (
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" +" +
"http://www.w3.org/TR/html4/loose.dtd\">\n" +
"<html> \n" +
"<head> \n" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; " +
"charset=ISO-8859-1\"> \n" +
"<title> Crunchify.com JSP Servlet Example </title> \n" +
"</head> \n" +
"<body> <div align='center'> \n" +
"<style= \"font-size=\"12px\" color='black'\"" + "\">" +
"Username: " + username + " <br> " +
"Password: " + password + " <br> " +
"Age: " + age +
"</font></body> \n" +
"</html>"
);
}
}
web.xml
<display-name>CrunchifyJSPServletExample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>com.crunchify.jsp.servlet.HelloCrunchify</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/CrunchifyServlet</url-pattern>
</servlet-mapping>
</web-app>
and if i tried to hit this localhost:9080/CrunchifyJSPServletExample/Crunchify.jsp
I'm getting HTTP Status 404.
Help will be appreciated.
Thanks
Add the server runtime to the web project to fix the HttpServlet not found Eclipse error. This will add all the JAR and ZIP files used by the servlet engine to the web project's runtime path. When this step is complete, rebuild the project and the HttpServlet not found Eclipse error message will go away.
For non-java developers, servlet is not suitable as they required to have a broad knowledge of Java servlet. HTML code is mixed up with Java code therefore, changes done in one code can affect another code. Writing HTML code in servlet programming is very difficult. It also makes servlet looks bulky.
The sendRedirect() method of HttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file. It accepts relative as well as absolute URL. It works at client side because it uses the url bar of the browser to make another request.
You need to take out your jsp from WEB-INF
and put it directly under WebContent
and it will work.
After looking at your web.xml and reading your comment you are using Crunchify.jsp to post data ,but servlet container is unable to find Crunchify.jsp in proper folder in the war, so you are getting this error 404, what you need to do is place Crunchify.jsp in same folder as index.jsp
As display in above image put Crunchify.jsp like WebPages --> Crunchify.jsp
now if you invoke http://localhost:9080/CrunchifyJSPServletExample/Crunchify.jsp
should work fine
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With