我下面有一个jps,下面的代码带有源和目标下拉列表,以及一个“ Execute”按钮,它将调用servlet。
服务端将根据所选的值执行某些操作。
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。
我在这里怎么了?
答案 0 :(得分:3)
我认为您的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>