This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 2 years ago.
I just started learning about Servlets. I followed the basic steps to create a new servlet project:
File -> new -> Dynamic Web Project -> new -> Servlet (AddServlet) with a basic print statement in the doGet()
package com.demo.servlets;
//all of my imports
#WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AddServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().print("Hello");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
I'll I've done is make the servlet but I can't get past this step. I've looked at a million tutorials and stack overflow answers. I made sure Tomcat 9.0 was installed and connected to my project correctly, made sure the servlet version was 4.0, made sure my build path was correct, made sure that my servlet was in a package.
I've also even tried using web.xml instead of the web annotations but I get the same 404error. . Ive been working on this for the past 72 hours, and I don't understand what the problem is.
You need to override doGet and doPost from HttpServlet. For this reason you need to add #Override annotation above the doGet and doPost.
Related
This question already has an answer here:
How to set my webapp to appear as ROOTfor localhost:8080
(1 answer)
Closed 1 year ago.
I'm only starting to learn Servlets.
I have a simple template code below.
I thought that after writing annotation "#WebServlet("/hello-servlet")" my page will be available by the URL "http://localhost:8080/hello-servlet".
The problem is that it is not available by that adress only by "http://localhost:8080/demo_war_exploded/".
I do know that the problem is connected with Tomcat configurations. In settings it is said that url is "http://localhost:8080/demo_war_exploded". And deployment is "war exploded".
How can I make the server available by the name in annotation?
#WebServlet("/hello-servlet")
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "Hello World!";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
// Hello
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>" + message + "</h1>");
out.println("</body></html>");
}
public void destroy() {
}
}
I found out the answer - you just need to delete application context from deployment
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
homePageController = (HomePageControllerV2) BeanLoader.getBean("homePageControllerV2");
}
}
whenever i hit to servlet from multiple machine. i still get the same instance of homePageController. is there any way can get diffrent instance of object homePageControllerper servlet request.
i have tired with changing the scope of bean to prototype. but still faced the same issue.
I'm trying to build a very simple Servlet using Groovy. Since it's just a single servlet plus a couple of gsp pages I don't want to integrate Grails into my project because I'm new to it. When I want the servlet run on Tomcat server(v7.0) I met such exception:
java.lang.ClassNotFoundException: groovy.lang.GroovyObject
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(ClassLoader.java:800)
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2904)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1173)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1681)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)...
Look at this thread Got java.lang.NoClassDefFoundError: groovy/lang/GroovyObject It seems the servlet.groovy is compiled fine but runtime env is not met.
My question is, how can I meet the runtime env without Grails(if possible)? I already have groovy-all-2.3.7.jar in my buildpath. Or could it be caused by version problems?
The servlet is extremely simple because I met the problem at very beginning.
import groovy.servlet.GroovyServlet
class Dispatcher extends GroovyServlet {
private static final long serialVersionUID = 1L;
public Dispatcher(){
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
println request;
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
println(request.getParameter("option"));
request.setAttribute("option", request.getParameter("option"));
Map<String,Object> result=new HashMap<>();
request.setAttribute("result", result);
println request;
// Forward to GSP file to display message
RequestDispatcher dispatcher = request
.getRequestDispatcher("/result.gsp");
dispatcher.forward(request, response);
}
}
To have the groovy-all-2.3.7.jar in the build path is not enough, it has to be in the WEB-INF/lib folder of the webapp, or in the Tomcat's lib folder, too.
This question already has answers here:
doGet and doPost in Servlets
(5 answers)
Closed 6 years ago.
public class CornelltaxiServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//resp.setContentType("text/plain");
//resp.getWriter().println("Hello, world");
}
}
From my understanding of doGet and doPost, it shouldn't matter where I put the "Hello, world" message. However, when I try to print it using the doPost method, it does not work. Could anyone explain this for me?
Also, from
void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Preprocess request: we actually don't need to do any business stuff, so just display JSP.
request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
}
request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
what does this do?
The doGet() method is called when HTTP GET is requested (e.g. when you type your servlets' URL in the browser). Then Hello, world will appear in the browser.
doPost() on the other hand will be used on HTTP POST. You need for example:
<form method="POST" action="/your/servlet"
When you submit such a form, you should see "Hello, world" (that is - when you uncomment it) in the browser as well.
As for your second question:
request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
This will forward request processing to hello.jsp. Basically, the contents of that file will be rendered instead of your Hello, world. Sending both content using resp.getWriter() and forwarding is a mistake. Pick one.
I am just beginning with Servlets and managed to have some servlets that act as individual URLs for populating a database for some dummy testing. Something of the form:
public class Populate_ServletName extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
//Insert records
//Print confirmation
}
}
I have about 6 such servlets which I want to execute in a sequence. I was thinking of using setLocation to set the next page to be redirected but was not sure if this is the right approach because the redirects should happen after the records have been inserted. Specifically, I am looking for something like this:
public class Populate_ALL extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
//Call Populate_1
//Call Populate_2
//Call Populate_3
//...
}
}
Any suggestions?
Use RequestDispatcher#include() on an URL matching the url-pattern of the Servlet.
public class Populate_ALL extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
request.getRequestDispatcher("/populateServlet1").include(request, response);
request.getRequestDispatcher("/populateServlet2").include(request, response);
request.getRequestDispatcher("/populateServlet3").include(request, response);
//...
}
}
Note: if those servlets cannot be used independently, then this is the wrong approach and you should be using standalone Java classes for this which does not extend HttpServlet. In your specific case, I think the Builder Pattern may be of interest.
The RequestDispatcher#forward() is not suitable here since it throws IllegalStateException when the response headers are already committed. This will be undoubtely the case when you pass the request/response through multiple servlets which each writes to the response.
The HttpServletResponse#sendRedirect() is absolutely not suitable here since it implicitly creates a brand new request and response, hereby trashing the original ones.
See also:
How do I call a second JSP servlet while in the first JSP servlet?
RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()
communication between remote servlets
It looks like what you may need is a service that each of the servlets can use to perform some work. Then the servlets are not depending one and another, but rather all using the service.
However, here is an explanation of forwarding or redirecting requests.