简体   繁体   中英

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

I have JSP page -

 <%@ 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>

And 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);

    }

}

First, I want to pass a value from JSP to the Servlet. Next, I want to make a conditional branch Finally, I want to display the jps screen

When I click on the tag(Sample1,Sample2,Sample4), I get a 404 error. The error message is "/action/ http://localhost:8080/action/jsp/registerForm.jsp "(if i click on the "sample1 and 2"). When i click on the Sample3, it work. Is the PATH I specified wrong? Please teach me.

I use... Eclipse2020 tomcat9 Java EE

enter image description here

I have JSP page -

 <%@ 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>

And 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);

    }

}

First, I want to pass a value from JSP to the Servlet. Next, I want to make a conditional branch Finally, I want to display the jps screen

When I click on the tag(Sample1,Sample2,Sample4), I get a 404 error. The error message is "/action/ http://localhost:8080/action/jsp/registerForm.jsp "(if i click on the "sample1 and 2"). When i click on the Sample3, it work. Is the PATH I specified wrong? Please teach me.

I use... Eclipse2020 tomcat9 Java EE

enter image description here

I modify jsp to

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);

    }

And modify servlet to

<body>

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

</body>

After changing the file location to https://gyazo.com/e79a8d083e5cc0c4ebb0d398e00a17ad , it work. Thanks to everyone for helping me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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