Null result being displayed using jsp

I am new to the concepts of dynamic web project .I have made the following two JSP and a one java class as just starting experiment. The following rae my code

user.java

package com;

public class User {

    private String name;
    private int Id;

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

index.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>Insert title here</title>
</head>
<body>

<form action="First.jsp">

name<input type="text" name="name" />
id<input type="text" name="Id" />


            <input type="submit" value="SUBMIT" />

<jsp:useBean id="user" class="com.User" />
            <jsp:setProperty name="user" property="*"/>     




</form>

</body>
</html>

and again First.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>Hello</title>
</head>
<body>
    <jsp:useBean id="user" class="com.User" />
user is :<%= user.getName() %><BR>
Id is :<%= user.getId() %><BR>



</body>
</html>

But its showing me null for both values.I know its very easy program .But I am not getting the concept correctly

Thanks

Upvotes: 1

Views: 1040

Answers (5)

Masudul
Masudul

Reputation: 21971

Use JSTL tag. It has default property for default value. If value is null then default value will be shown.

Upvotes: 0

venky
venky

Reputation: 422

You are submitting form data to First.jsp. But no where in First.jsp you are handling/ processing request parameters. So, Try something in First.jsp:

 <jsp:useBean id="user" class="com.User" scope = "request" />

<jsp:setProperty property="name" value="<%= request.getParameter("name") %>"name="user" />
<jsp:setProperty property="Id" value="<%= request.getParameter("Id") %> name="user" />

 Name: <jsp:getProperty property = "name" name="user" />
 Id : <jsp:getProperty property = "Id" name="user" />

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122018

You don't have any user object in session so all you need to do now is to make use of jsp:setProperty tag!.

The useBean tag will look for an instance of the "user" in the session.

If the instance is already there, it will update the old instance.Otherwise, it will create a new instance of user and put it in the session.

You previously doesn't put the user bean in the session.So new instance with null values vreated.

Bean Processing in JSP

Upvotes: 0

c.P.u1
c.P.u1

Reputation: 17094

Move

<jsp:useBean id="user" class="com.User" />
<jsp:setProperty name="user" property="*"/> 

from index.jsp to First.jsp

The request parameters are submitted to First.jsp and not index.jsp.

 <jsp:setProperty name="user" property="*"/>

populates user bean's properties with those from the request parameters. The form is submitted to First.jsp, which receives the request parameters.

Upvotes: 1

Ryan Stewart
Ryan Stewart

Reputation: 128899

Your jsp:useBean in First.jsp creates an instance of com.User using the default constructor, which sets both name and id to null. Therefore, you get null when you print their values.

Upvotes: 0

Related Questions