【问题标题】:Null value in request.getParameter when servlet is called from JSP [duplicate]从JSP调用servlet时request.getParameter中的空值[重复]
【发布时间】:2019-02-01 10:52:26
【问题描述】:

我在下面有一个 jps,下面的代码带有源和目标下拉菜单和一个“执行”按钮,它将调用一个 servlet。

servet 将根据选择的值执行一些操作。

JSP 代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title/>
    </head>
    <form action="MySourceEnv" method="POST">
        <select name="SourceEnv" >      
            <option>10.100.10.11</option>      
            <option>10.100.10.12</option>      
        </select>   
    </form>
    <form action="MyDestEnv" method="POST">
        <select name="DestEnv" >      
            <option>10.100.10.11</option>      
            <option>10.100.10.12</option>      
        </select>   
    </form>
    <body>
        <button onclick="location.href = 'http://localhost:7500/Project_1/JavaServlet';" id="RedirectButton" > Execute</button>
    </body>
</html> 

Servlet 代码:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class JavaServletClass extends HttpServlet {

    public void init() throws ServletException {
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String SourceEnvParam = request.getParameter("SourceEnv");
        out.println("<h1>" + SourceEnvParam + "</h1>");
        LogicMethod(SourceEnvParam);
    }

    private void LogicMethod(String SourceEnvParam) throws IOException {
        // Some logic here
    }

    public void destroy() {
    }
}

当单击执行按钮并调用 servlet 时,我将 request.getParameter("SourceEnv") 的值设为 Null。

我在这里做错了什么?

【问题讨论】:

    标签: java jsp servlets


    【解决方案1】:

    我认为在您的 html 代码中产生了问题。首先,您从您的身体中创建了两个具有两个动作的形式。但是你用

    定义了另一个动作
    <button onclick="location.href = 'http://localhost:7500/Project_1/JavaServlet';" id="RedirectButton" > Execute</button>. 
    

    试着像这样写你的jsp页面

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        <head>
    
            <title> name<title/>
        </head>
    
        <body>
          <form action="JavaServletClass" method="GET">
            <select name="SourceEnv" >      
                <option>10.100.10.11</option>      
                <option>10.100.10.12</option>      
            </select>  
           <select name="DestEnv" >      
                <option>10.100.10.11</option>      
                <option>10.100.10.12</option>      
            </select> 
    <button type="submit" value="Submit">Submit</button> 
    </form>
    </body>
    </html> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 2017-12-03
      • 2012-05-19
      • 2011-07-24
      • 2021-06-29
      • 1970-01-01
      • 2021-06-24
      相关资源
      最近更新 更多