有没有更好的方法可以使用将值从jsp传递到servlet,>

问题描述 投票:2回答:2

我有JSP页面-

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
  <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

  <a href="http://localhost:8080/action/RegisterUser">Sample1</a>
  <a href="http://localhost:8080/action/RegisterUser?action=done">Sample2</a>
  <a href="http://localhost:8080/action/jsp/registerDone.jsp">Sample3</a>
  <a href="/action/WebContent/jsp/registerForm.jsp">Sample4</a>

</body>
</html>

和servlet-

package example;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/RegisterUser")
public class RegisterUser extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String forwardPath = null;
        String action = request.getParameter("action");

        if(action == null) {
            forwardPath = "/action/WebContent/jsp/registerForm.jsp";
        }

        if(action != null && action.equals("done")) {
            forwardPath = "/action/WebContent/jsp/registerDone.jsp";
        }

        RequestDispatcher dispatcher = request.getRequestDispatcher(forwardPath);
        dispatcher.forward(request, response);

    }

}

首先,我想将值从JSP传递到Servlet。接下来,我要建立一个条件分支最后,我要显示jps屏幕

当我单击标签(Sample1,Sample2,Sample4)时,出现404错误。错误消息是“ / action / http://localhost:8080/action/jsp/registerForm.jsp”(如果我单击“ sample1 and 2”)。当我单击Sample3时,它起作用。我指定的PATH是否错误?请教我。

我使用...Eclipse2020雄猫9Java EE

enter image description here

我有JSP页面-

<...>[]

只需将jsp修改为此:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
  <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

  <a href="http://localhost:8080/action/RegisterUser">Sample1</a>
  <a href="http://localhost:8080/action/RegisterUser?action=done">Sample2</a>

</body>
</html>

这将起作用。

我将jsp修改为

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String forwardPath = null;
        String action = null;
        action = request.getParameter("action");

        if(action == null) {
            action = "miss";
            forwardPath = "registerForm.jsp";
        }

        if(action.equals("done")) {
            forwardPath = "registerDone.jsp";
        }

        RequestDispatcher dispatcher = request.getRequestDispatcher(forwardPath);
        dispatcher.forward(request, response);

    }

并将servlet修改为

<body>

  <a href="http://localhost:8080/action/RegisterUser">Sample1</a>
  <a href="http://localhost:8080/action/RegisterUser?action=done">Sample2</a>

</body>

将文件位置更改为https://gyazo.com/e79a8d083e5cc0c4ebb0d398e00a17ad,它起作用。感谢大家的帮助]

java html jsp servlets
2个回答
0
投票

只需将jsp修改为此:


0
投票

我将jsp修改为

© www.soinside.com 2019 - 2024. All rights reserved.