Folks,
I am working with the Java Servlet based web application and I used the Request URI to handle the multiple client requests into one servlet and used the conditional statement to apply the logics
ControlServlet.java
public class ControlServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
String strRequestURI = request.getRequestURI();
try{
if(strRequestURI.contains("/one")){
//Logic
}
if(strRequestURI.contains("/two")){
//Logic
}
if(strRequestURI.contains("/three")){
//Logic
}
if(strRequestURI.contains("/four")){
//Logic
}
}
catch(ServletException | IOException ex){
logs.errorLog(ex);
}
catch(Exception ex){
logs.errorLog(ex);
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>ControlServlet</servlet-name>
<servlet-class>com.main.ControlServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControlServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
F_one.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="POST" action="one.do" >
<!--First Form-->
</form>
</body>
</html>
F_two.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="POST" action="two.do" >
<!--Second Form-->
</form>
</body>
</html>
My problem is, I want to avoid each request checking using conditional statements. I can use the case(switch) but I am looking for Annotation based request mapping into Java Servlet so that I can handle both doGet() and doPost() methods without overriding it again. If anybody knows, please let me know the template so that I can update my project.
Thanks in advance!!