I have simple Srvlet class with following Get method:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Hello from Get method");
}
When I try to run this on Tomcat server , 8.5, I get the following message on browser : "Served at: /SimpleServletProject". This was the message which i removed from the default implementation of the Servlet.
And nothing is getting printed on Console.
Somehow my changes are not reflecting.
Try this one
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
System.out.println("Hello from Get method");
}
In your web.xml you should map your servlet like
<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>full path of class</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletName</servlet-name>
<url-pattern>/url</url-pattern>
Related
This question already has answers here:
Change default homepage in root path to servlet with doGet
(2 answers)
Difference between / and /* in servlet mapping url pattern
(5 answers)
Closed 2 years ago.
I wanted to load a servlet as the first page in my simple jsp appication. Therefore I added the servlet in this URL mapping.
<servlet>
<servlet-name>StudentController</servlet-name>
<servlet-class>com.stu.controller.StudentController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentController</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And this is the Get method of above mentioned servlet.
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getServletPath();
if (path.equals("/addstudent")) {
createStudent(request, response);
......
else {
searchAll(request, response);
}
}
protected void searchAll(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<StudentDTO> coList = null;
coList = StudentDAO.searchStudentList();
if (coList != null) {
request.setAttribute("stulist", coList);
} else {
request.setAttribute("msg", "No Record Found.");
}
RequestDispatcher rd = request.getRequestDispatcher("home.jsp");
rd.forward(request, response);
}
This is working as predicted, but the problem is I tried to create another servlet called CourseController and I mapped it in web.xml
<servlet>
<servlet-name>CourseController</servlet-name>
<servlet-class>com.stu.controller.CourseController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CourseController</servlet-name>
<url-pattern>/course/</url-pattern>
</servlet-mapping>
And the Get of the servlet is same as before.
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getServletPath();
if (path.equals("/addcourse")) {
createCourse(request, response);
......
}
And I tried to access the servlet using an anchor tag in my home.jsp page.
Create Student
<br/>
Create Course
But I keep getting 404 error while trying to connect CourseController servlet.
This some how return to the searchAll method in StudentController servlet. I understand something is wrong with the mapping here. But I don't know what is that.
Please help.
Thank you.
You need to configure unique url-patterns, so "/" cannot work if you have more than one servlet.
Assuming that your first servlet has url-pattern "/newstudent" you can configure it as welcome-file:
<welcome-file-list>
<welcome-file>newstudent</welcome-file>
</welcome-file-list>
I need to forward to a servlet that is been dynamically loaded from a jar by a custom class loader from the main servlet using an external configuration file. The servlet itself is not mapped in web.xml.
I have been able to load the servlet and construct a new instance using reflection and casting:
Object o = loadedClass.newInstance() ;
HttpServlet loadedServlet = (HttpServlet) o ;
I have initialized the servlet as:
loadedServlet.init(getServletConfig()) ;
And then implemented every do... method in the main servlet as:
loadedServlet.service(request, response) ;
Everything works as expected with the exception of the response getting output twice:
hello, world!hello, world!
Is not an issue of the loaded servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter printWriter = response.getWriter() ;
printWriter.write("hello, world!") ;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response) ;
}
Sorry if this is not enough specific. Any hint for what should I look at?
Nevermind, I found the solution myself.
A call of response.reset() after calling servlet.service() was all what I needed.
loadedServlet.service(request, response) ;
response.reset() ;
Currently I am working on Openxava frame work and it's new for me. I want to built a File download functionality in my current project, so for that i need a HttpServletResponse object. so please help me how do i get HttpServletResponse object in Openxava.
You can create a servlet and register it in servlets.xml (OpenXava adds the content of this file to web.xml upon deployment).
To enable the servlet for the user then create an action that implements IForwardAction.
For example servlet.xml might have:
<servlet>
<servlet-name>myDownloadServlet</servlet-name>
<servlet-class>org.webapp.test.MyDownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myDownloadServlet</servlet-name>
<url-pattern>/mydownload.do</url-pattern>
</servlet-mapping>
And the MyDownloadServlet class.
public class MyDownloadServlet extends HttpServlet {
/**
* Shows Hello World.
*/
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().write("Hello World");
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Finally your action
public class MyDownloadAction extends ViewBaseAction implements IForwardAction {
public String getForwardAction() {
return "/mydownload.do";
}
public boolean inNewWindow() {
return true;
}
}
Federico
i am having trouble with my code as i am accessing the logout servlet from a jsp page's hyperlink.
Jsp page link:
href="/logout"
logout Servlet:
public class logOut extends HttpServlet{
public void doGET(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/html");
System.out.println("log out servlet");
HttpSession session = req.getSession(false);
if (session != null) {
session.invalidate();
}
resp.sendRedirect("/signin.jsp");
}
}
but i am having the following error :
HTTP ERROR 405
Problem accessing /logout. Reason:
HTTP method GET is not supported by this URL
please help me.....
It is called doGet, not doGET.
The #Override annotation would have told you that.
Your method needs to be called
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { ... }
in order to be recognized - the uppercase letters make it fail.
I have written a simple test servlet and deployed it on websphere applications.
When i try to access the servlet it gives me following error
Error 404: SRVE0190E:
I have checked all the basic things like web.xml entry, servlet syntax etc.
Everything is correct.. The same servlet when deployed on Tomcat is working fine.
I also checked the
com.ibm.ws.webcontainer.invokefilterscompatibility property in Servers > Server > Web Container Settings > Web Container > Custom Properties .. it is set to true.
Not getting exactly what is the issue. If any one could help to resolve this issue
The serlvet that i created :
public class TestServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public TestServlet() {
super();
}
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.print("This is a testing servlet ... Please ignore");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
Entry in web.xml :
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.wizard.servlet.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
The war file which contains the servlet is deployed on one of the cells in Websphere server.
Thanks in advance.