如何在Apache Tomcat 8.0.27中使用@WebServlet Annotation

时间:2016-11-02 17:02:10

标签: java apache tomcat servlets

我使用 Java EE 7 Web Apache Tomcat 8.0.25

在我的Usr登录时,当我验证usr是否存在以及它是管理员还是普通用户时,我从Servlet(POST)返回" OK"或者" OK-ADM"然后我在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);
                }
            }
        });
    },

哪个是完全错的!出于安全原因......

所以我创建了一个 Servlet 来负责重定向到正确的页面, Usr 已经在此会话中因此我只需要验证它是管理员还是普通用户,我正在考虑做这样的事情:

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);
                }
            }
        });
    },

然后在 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();
        }
    }
}

这是通过IDE弹出的消息:

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

有没有办法在较新的Java应用程序上使用此功能?

任何帮助都将非常感谢,问候!

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。

首先,我的 javax.servlet.jar 不正确,所以下载更新的javax.servlet-api-3.1.0.jar解决了以下问题:

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

其次,在我的 Ajax 函数成功时(在验证数据库中是否存在 Usr 之前)我会像这样调用servlet:

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

document.location.href 调用 acceso.acc Servlet (如果你想这样做,你需要记住比你第一次需要会话中的用户

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();
        }
    }
}

注意:这样做我不需要在 web.xml

中加入 * .acc

非常感谢和问候。