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.
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 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>
I have a problem with setting url-pattern for servlet. If I set it to something like "users/*", after forwart() or include() to jsp, I get a redirect looping.
Here is my code:
#WebServlet("/users/*")
public class UserServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String[] pathParts = request.getPathInfo().split("/");
int id = Integer.valueOf(pathParts[1]);
request.setAttribute("userId", id);
request.getRequestDispatcher("user.jsp").include(request, response);
}
}
Just add "/" to your jsp page
getServletContext().getRequestDispatcher("/user.jsp").forward(request, response);
So what made difference here ???
If you directly specify user.jsp Server check user.jsp in default directory for example if Tomcat is used as webserver then server checks for user.jsp in /webapp folder (Where all applications resides.).
so where is user.jsp located ??? Its in your application for example "JSPTurorial". If you want reference user.jsp in your application you should give the relative path "/user.jsp" so that your server will check here "http://localhost:8080/JSPTutorial/user.jsp" as server is executing the files in /JSPTutorial directory else it will check here "http://localhost:8080/user.jsp" which will not be available at that path.
Try to change your servlet as follow:
#WebServlet("/users")
public class UserServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String[] pathParts = request.getPathInfo().split("/");
int id = Integer.valueOf(pathParts[1]);
request.setAttribute("userId", id);
getServletContext().getRequestDispatcher("/user.jsp").forward(request, response);
}
}
I am learning JSP and Servlets. Consider the following code inside the doPost method of a Servlet which forwards a HTTP request to a JSP -
RequestDispatcher view = request.getRequestDispatcher("/MyWebApp/MvcView.jsp");
I wonder what will happen if someone wants this servlet to forward the request to another jsp instead of the one above ? Does one have to change this code manually every time in their application ? How can one free oneself of all this manual work ?
One simple solution is to set the url as a parameter for your servlet:
<servlet>
<servlet-name>YourServlet</servlet-name>
<servlet-class>com.you.YourServlet</servlet-class>
<init-param>
<param-name>url</param-name>
<param-value>/MyWebApp/MvcView.jsp</param-value>
</init-param>
</servlet>
and the in the servlet:
public class YourServlet {
protected String url = null;
public void init(ServletConfig servletConfig) throws ServletException {
this.url = servletConfig.getInitParameter("url");
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
RequestDispatcher view = request.getRequestDispatcher(url);
}
}
then there is no need to recompile servlet code to chage the url.
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