accessing bean in servlet

hi to all,

i created a simple bean for login access that has getter and setter method...

I am trying to access the bean in a servlet .. i got error....

my bean code is ...

simple login page

<form method="post" action="/BeanServlet">

user Name :

<input type="input" name="username" >

Password :

<input type="password" name="password" >

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

</form>

package use;

public class UserData {

String username;

String password;

public void setUsername( String value )

{

username = value;

}

public void setPassword( String value )

{

password = value;

}

public String getUsername() { return username; }

public String getPassword() { return password; }

}

please fill in the Servlet code is

response.setContentType("text/html");

PrintWriter out = response.getWriter();

HttpSession session = request.getSession();

UseData ud = (UserData) session.getAttribute();

? what comes here

?

out.println(ud.getPassword(););

out.println(ud.getUsername());

[1198 byte] By [kannankallia] at [2007-11-27 10:56:27]
# 1

> i got error....

There are countless different kinds of errors and you don't even mention the error message and the relevant trace in here?

Error messages are not to be ignored. They are helpful in solving the problems. The only small step for you is to *interpret* and *understand* the error.

BalusCa at 2007-7-29 12:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

So, you have a jsp login page,

<form method="post" action="/BeanServlet">

user Name :

<input type="input" name="username" >

Password :

<input type="password" name="password" >

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

</form>

a class for handle the data,

package use;

public class UserData {

String username;

String password;

public void setUsername( String value )

{

username = value;

}

public void setPassword( String value )

{

password = value;

}

public String getUsername() { return username; }

public String getPassword() { return password; }

}

and a servlet,

response.setContentType("text/html");

PrintWriter out = response.getWriter();

HttpSession session = request.getSession();

UseData ud = (UserData) session.getAttribute();//do you get anything here? The form data comes in the request

? what comes here

?

out.println(ud.getPassword(););

out.println(ud.getUsername());

Post your complete servlet (and use the code tags)

Manuel Leiria

manuel.leiriaa at 2007-7-29 12:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

sorry ...

From Form how can i access the data in servlet ?

My servlet code is

response.setContentType("text/html");

PrintWriter out = response.getWriter();

UseData ud; (I create an object reference of Bean class , i am trying to access the object )

out.println(ud.getPassword(););

out.println(ud.getUsername());

I got a null pointer exception ........

Message was edited by:

kannankalli

Message was edited by:

kannankalli

Message was edited by:

kannankalli

kannankallia at 2007-7-29 12:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

A NullPointerException will occur when you want to access an uninstantiated object reference.

Go to fix it.

BalusCa at 2007-7-29 12:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

now i instantiated the object reference .. now i got

null

null

which does not take a value from form

kannankallia at 2007-7-29 12:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

sorry kannankalli, but this is what i understood from your question, that you want to take the information of a form from a servlet,,

and this is the servlet code:

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.*;

import javax.servlet.http.*;

public class Servlet extends HttpServlet {

private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

public void init(ServletConfig config) throws ServletException {

super.init(config);

}

public void doPost(HttpServletRequest request,

HttpServletResponse response) throws ServletException,IOException {

String retrievedUserName = request.getParameter("userNameField");

String retrievedPassword = request.getParameter("passwordField");

response.setContentType(CONTENT_TYPE);

PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<head><title>Servlet</title></head>");

out.println("<body>");

out.println("

The userName is:

" + retrievedUserName);

out.println("

The password is:

" + retrievedPassword);

out.println("</body></html>");

out.close();

}

}

notice request.getParameter("userNameField"); the userNameField is the same name as the textField in the html page, and so the password...

I hope i reached your point..(^_^);

QussayNajjara at 2007-7-29 12:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

i want to take a data from bean ?

i am accessing the data form data from bean it produces null .....

kannankallia at 2007-7-29 12:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

I think I had the same problem but I solved it by looking at this link.

http://forum.java.sun.com/thread.jspa?threadID=5185310&tstart=285

ripita at 2007-7-29 12:03:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...