File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes request.getattribute in servlet is null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "request.getattribute in servlet is null" Watch "request.getattribute in servlet is null" New topic
Author

request.getattribute in servlet is null

Mark Curlette
Ranch Hand

Joined: May 26, 2011
Posts: 46

Hello Friends,
I have a registration.jsp which calls register.jsp which in turn called my doRegister.java servlet.


1 ) In registration.jsp , i have a form contained two form fields named name and address

2) when i submit the form, it goes to register.jsp.
I am about to do request.getParameter ("name"),
and request.getParameter("address") to get the two values.

But when i called the

<jsp:useBean id="doRegister" class="org.test.DoRegister" />


DoRegister.returnRegistrationValue(request);

from my register.jsp to my DoRegister servlet,

3) the request.getParameter ("name") and request.getParameter ("address") are bot null.


So please advice me what i need to do in order to obtain name and address in my DoRegister servlet.


Thanks in advance,



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

When you say that register.jsp "calls" your doRegister servlet, exactly what does that mean? (It isn't standard terminology.)

(1) The JSP forwards to the servlet.

(2) The JSP redirects to the servlet.

(3) The JSP generates HTML which contains a link which is used to send a request to the servlet.

Only in case (1) are the JSP and the servlet in the same request. In the other two cases a new request is created for the servlet, and it won't contain any attributes set in the original request.

Edit: I just noticed this line in your post:


Did you say that DoRegister is a servlet??? This isn't a meaningful thing to do if that is the case.
Mark Curlette
Ranch Hand

Joined: May 26, 2011
Posts: 46

First, Thank you for the reply Paul.
and second , yes it was exactly what you said it was not meaning at all, but i don't know the best way to approach it so i'd like to seek your help.

My register.jsp contained

<jsp:useBean id="doRegister" class="org.test.DoRegister" />


DoRegister.returnZipcode(request);



and my doRegister.class contained

public class DoRegister {

private ConnectionPooling conn;
private Connection con;
private PreparedStatement pst = null;
private boolean visited = false;
private String logBuf = "";

public DoRegister() {
}

public String returnZipcode(HttpServletRequest request)
throws SQLException, NamingException, ClassNotFoundException {

String retZipcode = null;

HttpSession session = request.getSession();

synchronized (session) {



try {
conn = new ConnectionPooling();
con = conn.getConnection();

String sql = "select distinct Zipode"
+ " where name = ?"
+ " and address = ?";


pst = con.prepareStatement(sql);
pst.setString(1, (String)request.getParameter ("name"));
pst.setString(2, (String)request.getParameter ("address"));
ResultSet rs = pst.executeQuery();
while (rs.next()) {
retZipcode = rs.getString(1);

}


pst.close();
con.close();
} catch (SQLException sqlex) {
sqlex.printStackTrace();
log("Problem with SQL Exception");
} catch (Exception ex) {
} finally {
return retZipcode;

}
=============================================================================================

The problems are the request.getParameter for both name and address in DoRegister are both null,

and secondly, as you stated earlier and i would like to verify again to make sure that i understand that the register.jsp and DoRegister servlet are in two seprate requests, right? therefore the request.getParameter for name and address are now shown up in the DoRegister , is this correct ?

In that case, could i change request.getparameterfor name and address in DoRegister to request.getAttribute for both name and address in DoRegister, then will it work?

I would like to explore all options and to seek your help .

Thanks!
Mark Curlette
Ranch Hand

Joined: May 26, 2011
Posts: 46

First, i would like to use code tag to post my question.
Thank you for the reply Paul.
and second , yes it was exactly what you said it was not meaningful, but i don't know the best way to approach it so i'd like to seek your help.

My register.jsp contained

=============================================================================================

The problems are the request.getParameter for both name and address in DoRegister are both null,

and secondly, as you stated earlier and i would like to verify again to make sure that i understand that the register.jsp and DoRegister servlet are in two seprate requests, right? therefore the request.getParameter for name and address are not shown up in the DoRegister , is this correct ?

In that case, could i change request.getparameterfor name and address in DoRegister to request.getAttribute for both name and address in DoRegister, then will it work?

I would like to explore all options and to seek your help .

Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

Mark Curlette wrote:The problems are the request.getParameter for both name and address in DoRegister are both null,

Then they are not being submitted from the HTML page. (JSP just creates an HTML page to send to the browser, so once it gets to the browser, all JSP-ness is gone.)

Are they form elements? Are you constructing your own URL?

and secondly, as you stated earlier and i would like to verify again to make sure that i understand that the register.jsp and DoRegister servlet are in two seprate requests, right?

Of course. The JSP executes to send the HTMl to the browser, at which point that request goes by-by. Any submission from the browser has to be a new request.

therefore the request.getParameter for name and address are not shown up in the DoRegister , is this correct ?

getParameter simply grabs submitted information. It's orthogonal to how the HTML was originally created.

In that case, could i change request.getparameterfor name and address in DoRegister to request.getAttribute for both name and address in DoRegister, then will it work?

You really need to get a handle on basic concepts. parameters and attributes (more properly called scoped variables) are completely disjoint and separate concepts.

parameters = submissions from the browser

scoped variables = server side information stored in a scope

Which do you intend? You cannot just substitute them for each other.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

You keep referring to your DoRegister class as a servlet. It isn't a servlet, so please stop doing that. It's very confusing.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

OK, yeah. Now I am thoroughly confused.

Why is this a bean and not a servlet? Why aren't you following established patterns of use?
Mark Curlette
Ranch Hand

Joined: May 26, 2011
Posts: 46

Thank you so much Bear for your explanation. It helps me to see why my two fields name and adress in Doregister are null because they are actually 2 new requests.

One request from Registration.jsp to Register.jsp

and second request from register.jsp to doRegistration bean so the request.getParameter are gone; so i need to form a new request from my register.jsp to doRegistration.

In my case , i would like to retained those two name and address fields to use them later in my session bean . I'd like to ask if i could do the following :

1) Set attribute for name and address in my jsp page


2) So later on , in my Doregister , i can do the Session.getAttribute() for both name and address.

I don't know if doing this will satisfy the concept of forming a new request from register.jsp to DoRegister ? Please check


Thanks!


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

I'm still confused as to why you have processing code in a JSP bean? That flies in the face of all that is good and proper.
Mark Curlette
Ranch Hand

Joined: May 26, 2011
Posts: 46

Sorry Bear and Paul for the confusion, The reason why i am trying to keep processing code in the java bean b/c there are some old codes that i need to keep it running
. I understand it is bad and cumbersome. Would you any suggestions. Thanks in advance.
Murty Medisetti
Greenhorn

Joined: Jan 23, 2010
Posts: 23
How you are calling the second JSP Page? Are you using the Include/Forward?

If you are using include you can pass parameters to the second JSP
<jsp:include page="somePage.jsp">
<jsp:param name="name1" value="<%= someExpr1 %>" />
<jsp:param name="name2" value="<%= someExpr2 %>" />
</jsp:include>

Otherwise Use Scriptlets to set the attributes in scope variables.
Ex: request.setAttribute("name", value);

You can get these values in Servlets with request.getAttribute();
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

Murty Medisetti wrote:Otherwise Use Scriptlets to set the attributes in scope variables.

NO! Never use scriptlets for anything!

Scriptlets are antiquated and discredited technology from almost a decade ago! Their use, and encouraging their use, is irresponsible in 2011.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

Mark, you might benefit from reading a couple of articles that I wrote for the JavaRanch Journal:
  • The Secret Life of JSPs
  • The Front Man

  • Mark Curlette
    Ranch Hand

    Joined: May 26, 2011
    Posts: 46

    Thank you Bear, Murty , and Paul for all of your help.
    I read about Front Man as bear has suggested and i think it is the direction that i need to take for maintaining a single control servlet in my code.

    Thanks again for all of your guys help.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: request.getattribute in servlet is null