0

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!!

Prajwal Bhat
  • 303
  • 2
  • 5
  • 21
  • Same question here: http://stackoverflow.com/questions/14886484/java-servlet-annotations – Aaron Chen Sep 30 '16 at 07:00
  • 1
    If you want to avoid testing inside one single `processRequest()` method there is no annotation that can help you. You should be writing three different servlets and three different `processRequest()` methods, then you can annotate each servlet with the required path. – user207421 Sep 30 '16 at 07:23
  • Possibly of related interest: [*Replace a context-param element with a Servlet 3.0 annotation*](https://stackoverflow.com/q/7970051/642706) – Basil Bourque Sep 23 '19 at 05:33

0 Answers0