【问题标题】:How to use @WebServlet Annotation with Apache Tomcat 8.0.27如何在 Apache Tomcat 8.0.27 中使用 @WebServlet 注解
【发布时间】:2016-11-02 17:02:09
【问题描述】:

我正在使用 Java EE 7 WebApache Tomcat 8.0.25

在我的 Usr 登录时,当我验证 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 已经在会话中点所以我只需要验证它是管理员还是普通 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 应用程序上使用此功能?

任何帮助将不胜感激,问候!

【问题讨论】:

  • 在引入注解时,您的构建环境听起来像是在 Java/EE 6 之前设置的,尽管它也可能是构建问题。检查您的设置以确保您的目标是 EE 6 或 7,并让我们知道您的构建方式。作为一个不相关的说明,像这样滚动您自己的安全性有点危险 - 如果有人知道 admLog.jsp URL,他们可以直接访问它吗?
  • 你不知道这是否有帮助,但这就是我拥有 web.xml xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="w3.org/2001/XMLSchema-instance " xsi:schemaLocation="xmlns.jcp.org/xml/ns/javaee xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

标签: java apache tomcat servlets


【解决方案1】:

我已经解决了问题。

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

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

其次,在我的 Ajax 函数成功时(在验证 Usr 是否存在于 DB 之前)我这样调用 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

非常感谢和问候。

【讨论】:

    猜你喜欢
    • 2011-09-26
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 2014-07-28
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    相关资源
    最近更新 更多