Krusing
Krusing

Reputation: 307

setProperty value from requested getParameter

I want to give property "host" the value of the requested parameter "ip", submitted using method Get in form.html

This is my 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>

This is the included file form.html

<form action="index.jsp" method="GET">
    Connect to IP: <input type="text" name="ip"/>
    <input type="submit" value="Connect">
</form>

This is the Java file 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;
    }
}

It returns the string "Got value Connect" instead of the value I type in the form.

Upvotes: 1

Views: 1942

Answers (1)

Xavjer
Xavjer

Reputation: 9254

First of all, to compare strings, you need to use method equals, as you will otherwise compare two object which will not be the same

Change:

request.getParameter("ip") == ""){%

To:

request.getParameter("ip").equals("")){%

then you want to set the property host to the parameter ip

Change:

<jsp:setProperty name="connect" property="host" value="Connect" />

To (As the name attribute of the textfield is ip and not Connect, that's the button):

<jsp:setProperty name="connect" property="host" param="ip" />

Upvotes: 2

Related Questions