Tomcat 404 when using servlets - java

So I am currently trying to test a project whose facets I have changed, namely, I added a dynamic web component to it. In order to do so I decided to do a basic html form, and associated servlet with it.
However, when I try to run it I get a 404. Interestingly enough, a project I had which worked fine today is now also facing a similar issue. Though my practice servlet, which is used a template for everything else, does not have this issue.
the form follows this format:
<form action="myServlet">
<input type = "submit"/>
</form>
the servlet simply returns a writer.println("Hello World");
Error message:
HTTP Status 404 - /userName/LoginServlet
type Status report
message /userName/LoginServlet
description The requested resource is not available.
Apache Tomcat/8.0.38
Edit, and now the one servlet that worked also stopped working throwing the same error. If it helps, I am trying to run it from eclipse, by right clicking on my html page and then selecting for it to run on my server.

Problem was rather simple to hidden, since the name was refactored, the servlet mapping in eclipse did not show this. Deleting and recreating the servlet fixed it.
At least for now.

Related

The webpage cannot be found - HTTP 404 JSP page

Experts,
My dynamic web project having 3 JSP page and 3 servlets .. and everthing was working fine till date.
when i executed the same project today, it was showing this below message.
The webpage cannot be found - HTTP 404
I deleted the whole project after trying multiple checks... now in web.xml if i remove the servlets section, its displaying the jsp content. but if i add the servlet back its throwing same page cannot be found :(
no error in project and i dont see any logs in eclipse... can someone please tel me why this is happening?
Appreciate your input. Thanks
Below are a few general suggestions on debugging web apps:
3 and 4 would be the first checks for 404.
Try to start the web app in the debug mode, and place the break point in the servlet method: any hit? (or just put a System.out.println in the Servlet);
Check the structure of the dynamic web project in Eclipse: are the compiled java classes of the servlets in there?
Check the context path (url) of the dynamic web project in eclipse project config: not accidentally changed?
Check the log files of the tomcat (access logs and catalina logs) your dynamic web project is associated with.

Forwarding a request from servlet to .jsp fails when deployed on remote server

I have been stuck with this problem for two days now and I cant find a solution.
I have this project structure as above:
I want to forward a request from Survey(it is a Servlet) to survey.jsp.
Now when I deploy this from IntelliJ on localhost the forward is made with success. Now i take the .war and use the Tomcat Apache manager to deploy it to a dedicated Server.
When i call dedicatedserverip:8080 the index.jsp loads properly as in localhost:8080. But when it comes to forward there are two cases:
When the name of .war file is different from survey.war when the forward happens I get error 404 . (In this case I think that the request is forwarded to dedicatedserverip:8080)
When the name of .war file is survey.war when forward happens it happens to load again index.jsp page. (in this case I think the request is forwarded to dedicatedserverip:8080/survey)
Below is the code I use to forward the request:
req.getRequestDispatcher("/survey.jsp").forward(req, resp);
Now my question is: Is there something done wrong? Or is there something that I must understand that I haven't? How can I fix it and get the needed result as in localhost?
Have you tried forwarding relative instead of absolute?
req.getRequestDispatcher("survey.jsp").forward(req, resp);
Then additionally it should be no problem if you change the name of your war file and with it the ServletContext of the application any more.
Finally, I solved the problem. It had nothing to do with the forward, the problem was in the submit form action. Action was action="/survey". I replaced with ${pageContext.request.contextPath}/survey . And it solved the problem. This explains why the index page was called when .war name was survey.warand error page when it was different.
Thank you for your support.

Add a servlet will disable all servlet

I'm new to JavaEE and I have a problem with java servlet.
I have a servlet works well. Then I want to add another one. However, once I added the second one, the first one no longer works and server always respond 404 without any content (Normally the 404 will return with a page, not it is just a white page)
There must be some problems with the second servlet and If I disable one line of code the first servlet works. However I haven't seen any error messages
Can someone tell me the reason of this and how can I debug the program. (I cannot debug the program because the request cannot map to the second servlet and no breakpoints work)
Thanks a lot.
Fallflame
UPDATE
Now it works, thanks to Danail Alexiev remind me that it is may be a configuration problem. I found an error that said "cannot convert the project to dynamic web 2.5" and I just do maven -> update project then it works.
Still cannot understand why.
This is the one line code that let all servlets don't works :
ServiceMetadata edm = odata.createServiceMetadata(new DemoEdmProvider(), new ArrayList<EdmxReference>());
all copied from Apache Olingo tutorial.

Error-page not working (with pic)

I'm working in Eclipse+tomcat. My custom made static error page is not showing up in tomcat when i click on url for non-existing pages. generated-error.html has a simple img src="404_man.jpg" tag. But I keep getting the error page shown on the pic.
My servlet version is 3.0 if i view it from manifest.mf.
Have you tried the error definition for generic errors, not just 404.
/general-error.html
And are you sure tomcat is running on port 8080?
The error page does not look like Tomcat7's default 'cant find page' page.
Some browser like IE needs atleast 512 bytes of data to render. Otherwise it will not render the response. Try adding more content in your error page.
I believe you have very less content in your error page.
Mozilla will work without any problem in this kind of situation.

How to know which is the JSP webpage that is showing my servlet

I have a project that I can see the JSP's... However Jboss shows me the servlets so in the address bar all i can see is:
"appservleer?=AppDev"
This is an example not the actual address...but with that info only how can I know which JSP is being displayed at that moment by JBoss.
or am I understanding this Jboss, servlet, JSP incorrectly?
Some guidance would be appreciated.
Thanks.
You cannot know which jsp is being displayed unless the developer decided to give hints about it.
A jsp is just a view technology that the servlet uses to render a response. That is, typically, the jsp will be parsed and html will be generated. However, the developer of the servlet may decide to write to the response himself. So nothing can really tell you if what you got came from a jsp or from another source, even if you had a url like www.mydomain.com/page.jsp. Nothing guarantees the response you are seeing in your browser was generated from a jsp.
The only way to know for sure which JSP is used for rendering is to look at the source code of the servlet that is mapped under (in your example) "appservleer".
Look in web.xml which servlet is mapped with this path, and open the source code of this servlet. Then figure out the execution path, starting with the doGet(...) or the doPost(...) method, and see to which JSP it is forwarded in the end...
Good luck.
You should always start with the web.xml file which will eventually lead you to the actual jsp file that is displayed. Start with the servlet mapping and check which servlet is invoked for the url. Then dig into the servlet to find out which jsp it is redirected to. If you are using any application framework then look for the implementation to findout the mapping. For e.g. in spring the spring configuration files or the controller classes tells you where the calls are redirected to.

Categories

Resources