I know that there were similar problems already, but non of the solutions worked for me. I checked the directories and I edited my `web.xml file a couple of times but it still does not work.
I am writing a simple servlet in Java running on Tomcat and I am getting the error:
HTTP Status 404 – Not Found
Type Status Report
Message /WorkshopForm/MainWorkshopForm
Description The origin server did not find a current representation
for the target resource or is not willing to disclose that one exists.
My servlet class is:
package workshop;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet(description = "This registration form", urlPatterns = {
"/WorkshopForm" })
public class WorkshopForm {
public class MyServlet extends HttpServlet implements Servlet {
private static final long serialVersionUID = 13425L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String participantName = request.getParameter("participantName");
String participantSurname = request.getParameter("participantSurname");
String participantEmail = request.getParameter("participantEmail");
PrintWriter writer = response.getWriter();
writer.println("Welcome" + participantName + " " + participantSurname + " " + participantEmail);
}
}
}
My web.xml:
Tree in Eclipse:
Is it a problem with the web.xml file? I am thinking that maybe I have some mismatch with names or paths but I tried to solved it already and no idea why it is not working.
The URL pattern /WorkshopForm matches only the exact URL path /WorkshopForm. If you want the servlet to also handle longer paths like /WorkshopForm/MainWorkshopForm, you need to change the URL pattern to /WorkshopForm/*. Then you can call request.getPathInfo() in your servlet code to obtain the variable part of the path.
Another alternative is to use some JAX-RS framework to handle the mapping from URL paths to Java methods that handle individual paths.
Related
I am doing a Spring website. The website has many pages visitors can get. It also allows visitors to fill and submit a few forms.
I would like to use a Spring interceptor (as a central place) to examine each web request from visitors. How can I decide whether a web request is a get request or a form post?
Thanks and regards!
You can use getMethod() as follows -
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#Component
public class MyInterceptor extends HandlerInterceptorAdapter {
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
System.out.println("Request Intercepted for : "
+ request.getRequestURI());
System.out.println("Request type : "
+ request.getMethod());
return true;
}
}
Form documentation
java.lang.String getMethod()
Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT. Same as the value of the CGI variable REQUEST_METHOD.
Returns:
a String specifying the name of the method with which this request was made
I create a servlet in my CQ5 application:
import java.io.IOException;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
#SlingServlet(
label = "Example Servlet",
paths = {"/bin/project/signin"},
methods = {"GET"},
extensions = {"html"},
metatype = false
)
public class SignInServlet extends SlingAllMethodsServlet {
private static final long serialVersionUID = 796802690004962223L;
#Override
protected void doGet(SlingHttpServletRequest request,
SlingHttpServletResponse response) throws ServletException,
IOException {
doPost(request, response);
}
#Override
protected void doPost(SlingHttpServletRequest request,
SlingHttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/plain");
response.getOutputStream().print("Sigin Servlet");
}
}
i try to call it via rest-client but it return:
No resource found for url: http://localhost:4502/bin/project/signin
I also open Sling Resource Resolver at Felix side to test if it available. But i received:
NonExistingResource, path=/bin/project/signin
/bin/ already config in Apache Sling Servlet/Script Resolver and Error Handler at Fellix
In the #SlingServlet annotation you've declared that this servlet supports only requests with .html extension, so you should hit following URL:
http://localhost:4502/bin/project/signin.html
If you don't want to use the extension, remove appropriate parameter from the servlet annotation.
Two ways to help debug this type of situation —
The ServletResolver will allow you to check which Servlet a given GET or POST request will resolve against.
If you find the request you made that generated the 404 in the Recent Requests tab, it should tell you exactly what properties it has found by Sling when trying to resolve it.
E.g. in your case, I presume the 404 is giving something like:
LOG Resource Path Info: SlingRequestPathInfo: \
path='/bin/project/signin', \
selectorString='null', \
extension='null', \
suffix='null'
Comparing this against the settings in your annotation, there's an extension='null' here that wouldn't match against your servlet — which is binding only against a 'html' extension, as Tomek rightly says above.
This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
I have a problem with my simple servlet that I am trying to run, Hello.java. I made it in eclipse, then placed the file it in the webapps/ServletTest/WEB-INF/classes folder and compiled it, creating the file Hello.class in the same folder. I then modified my web.xml file to map the servlet and tried to run it through the following address
http://localhost:8080/ServletTest/Hello
However, this did not work, giving the following error
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/7.0.42
The mapping in the web.xml file looks like this:
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Main.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
The code of the servlet:
package Main;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("/Hello")
public class Hello extends HttpServlet {
private static final long serialVersionUID = 1L;
public Hello() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String path = request.getContextPath();
String ip = request.getRemoteAddr();
out.print("<html>" +
"<title>Hello</title>" +
"Hello World"+ "<br>" +
"Your ip is: " + ip + "<br>" +
"Your path is: " + path
+ "</html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
The compiled Hello.class file should be in the folder
webapps/ServletTest/WEB-INF/classes/Main
since it's declared to be in package Main.
Also, you can see Tomcat's startup logs in /logs/catalina.out or /logs/catalina.log, depending.
Also, Suresh is right in the comments, use either a <servlet> declaration or #WebServlet. Don't use both.
Try to delete web.xml.
Annotation #WebServlet("/Hello") is enough for Tomcat 7+
If the root folder not having proper permission you will face this issue. So please check the root folder property and remove read only permission and add user to full access permission in security tab.
I'm currently trying to run a servlet that check a GET value and write the value.
Here my class :
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet extends HttpServlet {
/**
*
*/
public Servlet() {
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
final String myValue = request.getParameter("MyValue");
if (myValue != null && !myValue.isEmpty()) {
response.getWriter().write(myValue);
}
}
}
It's quit simple, right ?
I already tried many url to be able to see my result like the following :
- http://localhost:8080/Servlet/servlet/Servlet
- http://localhost:8080/Servlet/servlet/Servlet?MyValue=Test
- http://127.0.0.1:8080/Servlet/servlet/Servlet
- http://127.0.0.1:8080/Servlet/servlet/Servlet?MyValue=Test
Is there something wrong with my code or is it a problem with my eclipse?
Thx
You didn't tell anything about the problem symptoms, but I'll assume that you're getting a HTTP 404 error page on all the attempts, right?
You need to map the servlet on an URL pattern. First, you need to assure that the servlet class is placed in a package (we'll assume com.example in this answer).
If you're still on Java EE 5 (or even J2EE..), register it in the webapp's /WEB-INF/web.xml (Eclipse should have autogenerated one):
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>com.example.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
(the servlet name is basically the instance variable name, there's only one applicationwide; the servlet class is obviously the FQN; the URL pattern the webcontent-relative URL of the servlet)
Or when you're already on the latest Java EE 6, then annotate it with #WebServlet wherein you specify the URL pattern as value:
package com.example;
// ...
#WebServlet("/servlet")
public class Servlet extends HttpServlet {
// ...
}
Either way, it's basically telling that the servlet should listen on webcontent-relative URLs matching /servlet. So, assuming that your web context root path is /Servlet, then this should do:
http://localhost:8080/Servlet/servlet
In the future, it'd be easier if you created the servlet class by New > Servlet instead of New > Class, then this all will be automagically taken into account in the wizard.
See also:
Our Servlets wiki page - contains some Hello World examples - you can get to this page by hovering your mouse a while above the servlets until a black info box shows up and then clicking the info link therein.
You can find your Context Root by right clicking your project in eclipse -> Properties -> Web Project Settings.
I'm creating a program that communicates with a webservice, which is written with JSP and Struts. But when I want to create a new instance of the webservice I get the following error:
SEVERE: Servlet.service() for servlet [FrontController] in context with path [/P3_GUI] threw exception [Servlet execution threw an exception] with root cause
java.lang.IllegalArgumentException: com.sun.xml.messaging.saaj.soap.LocalStrings != com.sun.xml.internal.messaging.saaj.soap.LocalStrings
Which occurs in the following code of my ActionClass
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import vakws.Vak;
import vakws.VakService;
public class AddAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
AddForm myForm = (AddForm)form;
VakService service = new VakService();
Vak vakProxy = service.getVakPort();
boolean result = vakProxy.addVak(myForm.getVakName(), Double.parseDouble(myForm.getVakMark()));
if(!result){
return mapping.findForward("show_addError");
}
return mapping.findForward("show_addResults");
}
}
VakService and VakProxy is automatically generated using the wsdl document.
I'm developing in Eclipse and a deploying the program on a Tomcat server. The webserver runs with JAX-WS 2.2.3
Does anybody know a solution for this problem?
Thanks in advance!
You have multiple different versioned SAAJ libraries in your webapp's runtime classpath. SAAJ is already bundled with JDK. Probably you have some saaj-*.jar file in your webapp's /WEB-INF/lib which is conflicting with JDK-bundled SAAJ library. Cleanup it.