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.
<a href="${pageContext.request.contextPath}/newstudent">Create Student</a>
<br/>
<a href="${pageContext.request.contextPath}/course/newcourse/">Create Course</a>
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.