• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Java Web application: Unable to launch Servlet class from jsp page

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am creating a simple Java web application "Registration Form". I am using following hardware and softwares:
Machine: Amazon Linux 2 EC2 Instance.
Java version: 1.8.0_282
Maven: 3.6.3
Apache Tomcat: 7.0.76
Servlet: 3.1.0 (In pom.xml group ID: javax.servlet, artifactId: javax.servlet-api)
Note: I am not using any IDE. I am writing code using Linux vim utility.

I am following below steps in order to create Java web application project "Registration Form".
1. Create project directories and pom.xml using


2. Directory structure looks like below:


3. I have added servlet dependency in pom.xml
4. Write code in pom.xml, index.jsp, myStyle.css, register.jsp, guru_register.java, web.xml. Code is shown below.
5. When I execute "mvn clean package" it build the project successfully. After deployment to Tomcat server it display the register.jsp page correctly. After fill data and press submit button it throws error "HTTP Status 404 - /JavaWebApplication/jsp/guru_register". I do not understand why it is looking guru_register page in /jsp folder. Can you please see the code and help me to find the issue?

Note: Due to some limitation I cannot use Windows OS and any IDE so I am using Amazon Linux EC2 for practice. Thank you!

Code:

pom.xml


index.jsp


myStyle.css


register.jsp


guru_register.java


web.xml
 
Master Rancher
Posts: 4507
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should the action item have an extension of jsp?  


There would need to be a guru_register.jsp file.
 
jnrohit Jain
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to implement MVC model I created guru_register.java file in ~/Controller directory. If I convert this file into .jsp and move it into ~/jsp directory then it won't solve the purpose of MVC model. What is the way to call class file of guru_register.java located in ~/Controller directory?

Thanks!
 
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you don't need to change your servlet into a JSP. You just need to link to it correctly; the URL you want to link to includes the webapp's context path, which you haven't included. Something like this:

 
Saloon Keeper
Posts: 24558
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Time for me to get pedantic again.

You cannot "launch" a Servlet (or servlet class). Launching implies that you have created an executable process (or thread). Servlets don't execute - they are executed. That is, when a web request comes in and the request URL matches the servlet's URL patterns(s) defined in WEB-INF/web.xml (or servlet annotations), then the webapp server will pull a Thread from its Thread Pool and tell that Thread to invoke the low-level servlet process method, which then typically calls your actual servlet's doGet(), doPost(), or whatever.

In other words, servlets do absolutely nothing unless a request has been handed to them, then they should handle that request as quickly as possible and return.

In a typical web MVC architecture, servlets act as the dispatchers (Controllers) for incoming requests - both raw URLs and FORM posts (GET and POST). The servlet would then invoke the proper business logic and - since this is MVC - it would then forward to a JSP whic renders the View. That's different from bare servlet operation in that in bare servlets, the servlet is responsible for creating and returning the View response stream itself. Forwarding to a JSP allows you to template the view instead of brute-force coding the response HTML - which gets old really quickly.

Also a quick reminder on JEE URLs. A URL has several components:

1. The server routing and protocol information - https://coderanch.com:8080 for example.
2. The application context path. This tells the webapp server which of several webapps it may be running to send the request to. For example "/mywebapp".
Together they are returned by Pauls's "${pageContext.request.contextPath}" variable reference.

3. At this point, we know which server and which webapp will receive the request. The next element is the servler/JSP URL pattern, I mentioned initially, followed by optional request arguments, which I won't bother mentioning here.
 
jnrohit Jain
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. It worked.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic