weixin_33735077 2017-04-16 22:00 采纳率: 0%
浏览 36

Java servlet映射

Hello I have an Embedded Tomcat java application. The tomcat starts and the index.jsp is displayed.

Now i have an An ajax Request which looks like this:

function auth(){
username = document.getElementById('login_username').value;
password = document.getElementById('login_pw').value;

if(username.length > 0 && password.length > 0){
   $.ajax({
        method: "POST",
        url: '/login',
        data: 'username=' + username + '&password=' + password
    }).done(function(response){
        console.log(response);
        response = response.split(',');
        if(response.length == 2){
            window.location = response[1];
        }
    });
}

}

The auth() works. But every time i get a 404 error.

The servlet which should get the Request looks like this:

@WebServlet(
        name="login",
        urlPatterns = {"/login","sites/login","login"}
)
public class LoginServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;
    private ServletContext context;

    public LoginServlet(){
        super();
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Log.log("get");
        resp.setContentType("text/html");  
        PrintWriter out=resp.getWriter(); 
        Page p = new Page();
        p.getBody().setContent("GET");
        out.print(p.create());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Log.log("Post");
        resp.setContentType("text/html");  
        PrintWriter out=resp.getWriter(); 
        Page p = new Page();
        p.getBody().setContent("POST");
        out.print(p.create());
    }

    public void init(ServletConfig config)throws ServletException{
        this.context = config.getServletContext();
        Log.log("Login servlet Initialized");
    }
}

My question is, why is this not working. Have i to add The servlett to the Tomcat with the tomcat.addServlet(string,string,string) method?;

It works if i create a login.jsp in the base folder of my webapp and if i change the url of the ajax request to login.jsp.

Thanks

  • 写回答

2条回答 默认 最新

  • ℙℕℤℝ 2017-04-16 22:56
    关注

    The problem is with your parameter url: '/login' in the $.ajax() call which makes the request in http://localhost:8080/login because of the / (slash symbol) in the beginning.

    You are most likely expecting it to be something like http://localhost:8080/MyServletApp/login. To achieve this remove the / from the url.

    The ajax function call would look like this :

    $.ajax({
        method: "POST",
        url: 'login',
        data: 'username=' + username + '&password=' + password
    }).done(function(response){
        console.log(response);
        response = response.split(',');
        if(response.length == 2){
            window.location = response[1];
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度