【问题标题】:How can i call servlet from jsp (HTTP Status 404 – Not Found) [duplicate]我如何从 jsp 调用 servlet(HTTP 状态 404 - 未找到)[重复]
【发布时间】:2019-11-11 14:15:38
【问题描述】:

我正在创建一个登录 .JSP,它在登录按钮上调用 login.java(servlet)。但是 JSP 无法调用 servlet 文件并报错。

Login.JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>login</title>
</head>
<body>
<form action="Login" method="get">
    Enter Username:<input type="text" name="uname"><br>
    Enter Password:<input type="password" name="upass"><br>
    <input type="submit" value="login">
</form>
</body>
</html>

Login.java(servlet 文件)

package com.login;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String uname=request.getParameter("uname");
        System.out.print("uname is"+uname);
        String upass=request.getParameter("upass");
        if (uname.equals("meet") && upass.equals("1234")) {
            response.sendRedirect("welcome.jsp");
        }
        else {
            response.sendRedirect("login.jsp");
        }
    }


}

错误信息 HTTP 状态 404 - 未找到

类型状态报告

留言/登录

描述 源服务器没有找到目标资源的当前表示或不愿意透露存在。

【问题讨论】:

    标签: java jsp tomcat servlets file-not-found


    【解决方案1】:

    只需在 jsp 请求中使用 getConextPath 即可

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
             pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="ISO-8859-1">
      <title>login</title>
    </head>
    <body>
    <form action="<%=request.getContextPath()%>/Login" method="get">
      Enter Username:<input type="text" name="uname"><br>
      Enter Password:<input type="password" name="upass"><br>
      <input type="submit" value="login">
    </form>
    </body>
    </html>
    

    希望我能有所帮助;但是关于使用 Scriptlets 的 BalusC 自 2003 年推出的 JSP 2.0 以来正式不鼓励(!!)。请不要鼓励初学者使用不良做法。正确的做法是改用 EL ${ ... }。 -

    所以你可以这样写,并且总是尝试以这种方式使用它

      <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
                 pageEncoding="ISO-8859-1"%>
        <!DOCTYPE html>
        <html>
        <head>
          <meta charset="ISO-8859-1">
          <title>login</title>
        </head>
        <body>
        <form action="${requestScope.getContextPath}/Login" method="get">
          Enter Username:<input type="text" name="uname"><br>
          Enter Password:<input type="password" name="upass"><br>
          <input type="submit" value="login">
        </form>
        </body>
        </html>
    

    最好知道坏编码和写编码,希望对你也有帮助。

    【讨论】:

    • Scriptlets &lt;% ... %&gt; 自 2003 年推出的 JSP 2.0 以来已被正式禁止(!!)。请不要鼓励初学者使用不良做法。正确的做法是改用EL ${ ... }
    • 是的@BalusC 你是对的,这是我的错。
    猜你喜欢
    • 2019-07-30
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    相关资源
    最近更新 更多