Ramiro Arizpe Ramiro Arizpe - 1 month ago 7
Apache Configuration Question

How to use @WebServlet Annotation with Apache Tomcat 8.0.27

I'm using Java EE 7 Web with Apache Tomcat 8.0.25

On my Usr login, when I verify if the usr exist and it's administrator or normal usr I return from Servlet (POST) an "OK" or "OK-ADM" then I do this on Ajax:

login: function () {
$.ajax({
type: "POST",
cache: false,
timeout: 30000,
dataType: "json",
url: "loginUsr.usr",
data: {
usr: $("#usr").val(),
pass: $("#pass").val()
},
success: function (data)
{
if (data.estatus === "OK")
{
setInterval(function(){
location.href = "http://localhost:8080/WebPage/uploadPDF.jsp"; //HERE IS THE SEVERE THING
}, 3000);
} else if(data.estatus === "OK-ADM"){
setInterval(function(){
location.href = "http://localhost:8080/WebPage/admLog.jsp"; //HERE IS ANOTHER SEVERE THING
}, 3000);
}
}
});
},


Which is totally wrong! For security reasons...

So it occurred to me create a Servlet to be responsible for redirect to the correct page, the Usr is already in session at this point so I just need to validate if it's admin or common usr, I was thinking on doing something like this:

login: function () {
$.ajax({
type: "POST",
cache: false,
timeout: 30000,
dataType: "json",
url: "loginUsr.usr",
data: {
usr: $("#usr").val(),
pass: $("#pass").val()
},
success: function (data)
{
if (data.estatus === "OK")
{
setInterval(function(){
document.location.href = 'access.acc'; //The New Servlet
}, 3000);
} else if(data.estatus === "OK-ADM"){

setInterval(function(){
document.location.href = 'access.acc'; //The New Servlet
}, 3000);
}
}
});
},


Then in the Servlet:

@WebServlet(name = "access", urlPatterns = {"*.acc"}) //BUT THIS DOESN'T WORK
public class access extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String accion = request.getServletPath();
response.setContentType("text/html;charset=UTF-8");
HttpSession session = request.getSession(true);
Usuario usr = null;

try {
try{
usr = (Usuario)request.getSession().getAttribute("usuario");
} catch(Exception e){
request.getRequestDispatcher("index.jsp").forward(request, response);
}
if("/access.acc".equals(accion)){
PrintWriter out = response.getWriter();
if(usr.getTipoUsuario() == 1 || usr.getTipoUsuario() == 2 || usr.getTipoUsuario() == 3) {
request.getRequestDispatcher("admLog.jsp").forward(request, response);
} else if(usr.getTipoUsuario() == 0){
request.getRequestDispatcher("uploadPDF.jsp").forward(request, response);
}
out.close();
} else if("/salir.acc".equals(accion)){
if(null == usr){
PrintWriter out = response.getWriter();
session.invalidate();
request.getRequestDispatcher("index.jsp").forward(request, response);
out.close();
}
}
} catch(Exception e){
e.printStackTrace();
}
}
}


This is the message pops over IDE:

package javax.servlet.annotation does not exist
----
(Alt-Enter shows hints)


It's there a way to use this functionality on newer Java Application?

Any help would be greatly appreciated, Regards!

Answer

I have solve the problem.

First, My javax.servlet.jar it was not correct, so downloading the updated javax.servlet-api-3.1.0.jar solves the problem of:

package javax.servlet.annotation does not exist
----
(Alt-Enter shows hints)

Second, on my Ajax function when it's success(before validating if the Usr exist on DB) I call the servlet like this:

success: function (data)
{
    if (data.estatus === "OK")
    {
        setInterval(function(){ 
            document.location.href = 'acceso.acc'; //here is some magic.
        }, 3000);
    } else {
        //error message
    }
}

The document.location.href calls the acceso.acc Servlet (If you want to do this you need to remember than you first need the user on a session)

package some.package.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.al.entity.Usuario;
import javax.servlet.annotation.WebServlet;

@WebServlet(name = "access", urlPatterns = {"*.acc"}) //This is how I receive all the .acc routes
public class access extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String accion = request.getServletPath();
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession(true);
        Usuario usr = null;
        try {
            try{
                usr = (Usuario)request.getSession().getAttribute("usuario");
            } catch(Exception e){
                e.printStackTrace();
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }
            if("/acceso.acc".equals(accion)){
                    PrintWriter out = response.getWriter();
                    if(usr.getTipoUsuario() == 1 || usr.getTipoUsuario() == 2 || usr.getTipoUsuario() == 3) { 
                        request.getRequestDispatcher("admLog.jsp").forward(request, response);
                    } else if(usr.getTipoUsuario() == 0){
                        request.getRequestDispatcher("uploadPDF.jsp").forward(request, response);
                    }
                    out.close();
            } else if("/salir.acc".equals(accion)){
                if(null == usr){
                    PrintWriter out = response.getWriter();
                    session.invalidate();
                    request.getRequestDispatcher("index.jsp").forward(request, response);
                    out.close(); 
                }
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

Note: Doing this I don't need to include the *.acc on the web.xml

Many thanks and Regards.

Comments