我想为属性“host”
提供所请求参数“ip”
的值,使用form.html
中的Get方法提交
这是我的index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ip</title>
</head>
<body>
<jsp:directive.include file="form.html"/>
<% if (request.getParameter("ip") == null ||
request.getParameter("ip") == ""){%>
Not connected!
<% } else {%>
<jsp:useBean id="connect" class="test.Ip" />
<jsp:setProperty name="connect" property="host" value="Connect" />
<jsp:getProperty name="connect" property="host" />
<% }%>
</body>
</html>
这是包含的文件form.html
<form action="index.jsp" method="GET">
Connect to IP: <input type="text" name="ip"/>
<input type="submit" value="Connect">
</form>
这是Java文件Ip.java
package test;
public class Ip{
private String host;
public String getHost(){
return("Got value " +host);
}
public void setHost(String host){
this.host = host;
}
}
它返回字符串“Got value Connect”,而不是我在表单中输入的值。
最佳答案
首先,要比较字符串,您需要使用方法 equals,否则您将比较两个不同的对象
更改:
request.getParameter("ip") == ""){%
致:
request.getParameter("ip").equals("")){%
那么你要将属性host设置为参数ip
更改:
<jsp:setProperty name="connect" property="host" value="Connect" />
To(由于文本字段的名称属性是 ip 而不是 Connect,所以就是按钮):
<jsp:setProperty name="connect" property="host" param="ip" />
关于java - 从请求的 getParameter 中设置属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33828988/