繁体   English   中英

有没有更好的方法将值从 jsp 传递给 servlet 使用<a href></a>

[英]Is there any better way to pass the value from jsp to the servlet using <a href>

我有 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>

和小服务程序-

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 tomcat9 Java EE

在此处输入图像描述

我有 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 和 2”)。 当我单击 Sample3 时,它可以工作。 我指定的 PATH 是否错误? 请教我。

我用... Eclipse2020 tomcat9 Java EE

在此处输入图像描述

我修改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 后,它起作用了。 感谢大家帮助我

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM