java bean servlet

is it possible to access a bean in servlet ?

if yes ...

please solve my problem

i am trying to access a form data from html page..

simple form

<form method="post" action="Beanservlet" >

<input type="text" name=username >

</form>

BeanServlet.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import use.UserData;

publicclass BeanServletextends HttpServlet

{

protectedvoid doGet (HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException

{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

UserData ud =new UserData();

String st =ud.getUsername();

out.println(st);

}

protectedvoid doPost (HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException

{

doGet(request,response);

}

}

Bean Class

package use;

publicclass UserData{

private String username;

publicvoid setUsername( String value )

{

username = value;

}

public String getUsername(){return username;}

}

in my beanservlet page i got null instead of printing the value i accessed from html ..

null

[2489 byte] By [kannankallia] at [2007-11-27 11:24:23]
# 1

UserData ud = new UserData();

String st =ud.getUsername();

out.println(st);

do u know what u were doing on the above code? u're trying to create an instance of UserData without setting any value into its properties, and then you get the username from this empty instance, surely you will get null for the name...

in order to retrieve data from a jsp page, u'll need to ensure that u're using a form, and the data u intended to retrieve is specified inside this form. and then, in your servlet class, doGet method, use HttpServletRequest.getParameter(String name), where the name is the name you specified in the input tag. once u've done that, it should display the name correctly....

hope this help.

JWKC-5ivea at 2007-7-29 15:57:59 > top of Java-index,Desktop,Developing for the Desktop...