我无法从jsp调用servlet(HTTP状态404 –找不到)

我正在创建一个登录.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 –找不到

类型状态报告

消息/登录

描述原始服务器找不到目标资源的当前表示,或者不愿意透露该资源的存在。

我无法从jsp调用servlet(HTTP状态404 –找不到)

ashinelee 回答:我无法从jsp调用servlet(HTTP状态404 –找不到)

只需在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>

希望我会有所帮助;但是自2003年引入JSP 2.0以来,正式不鼓励使用Scriptlet 的BalusC(!!)。请不要鼓励初学者使用不良做法。正确的做法是改用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>

最好了解错误的编码和写入编码,希望对您有帮助。

本文链接:https://www.f2er.com/3123587.html

大家都在问