This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes getParameter gives null value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "getParameter gives null value" Watch "getParameter gives null value" New topic
Author

getParameter gives null value

Ashwini Kulkarny
Greenhorn

Joined: Aug 12, 2008
Posts: 9
Hi,

I realize this is very much a beginner's level problem, but unfotunately I could not find any solution to it. i am coding a J2EE application using jsp and struts 1.3 for the frontend using RAD7.5.

My issue is that the value of request.getParameter is null in th srtuts action class.So, i created a sample application with 2 simple jsps and an action class. in the sample code, i have an input jsp with only 1 text field and a link 'next'. When I click this link, it calls the action. In the action, it reads the text field value using request.getParameter, and sets the value read to the request object. However, when I debug the application , I am getting null value for request.getParameter.
One more issue is that inspite of setting the method type as 'GET', I do not see the parameters in the url after it is submitted.
My code is as follows -

JSP 1 - start.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">
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="startform" method="GET">
<input type="text" name="txtstart"/>
<html:link action="/gonext">Next</html:link>
</form>
</body>
</html>


JSP - end.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>output</title>
</head>
<body>
<%=request.getAttribute("txtstart") %>
</body>
</html>

Struts-config.xml file -

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
</global-forwards>
<action-mappings>
<action path="/gonext" type="org.wd.testget.actions.GonextAction">
<forward name="success" path="/jsp/end.jsp">
</forward>
<forward name="failure" path="">
</forward>
</action>
</action-mappings>
<message-resources parameter="org.wd.testget.resources.ApplicationResources"/>
</struts-config>


Action Class -

package org.wd.testget.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
* @version 1.0
* @author
*/
public class GonextAction extends Action

{

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {

ActionMessages errors = new ActionMessages();
ActionForward forward = new ActionForward(); // return value

try {

String op = (String) request.getParameter("txtstart");
request.setAttribute("outputtext", op);


} catch (Exception e) {

// Report the error using the appropriate name and ID.
errors.add("name", new ActionMessage("id"));

}

// If a message is required, save the specified key(s)
// into the request for use by the <struts:errors> tag.

if (!errors.isEmpty()) {
saveErrors(request, errors);

// Forward control to the appropriate 'failure' URI (change name as desired)
//forward = mapping.findForward(failure");

} else {

// Forward control to the appropriate 'success' URI (change name as desired)
forward = mapping.findForward("success");

}

// Finish with
return (forward);

}
}

Can anyone please tell me -
1.why request.getParameter is returning null value?
2.why is the text parameter not showing in the url after it is submitted even though the method is 'GET'?

Would really really appreciate any help!

Thanks in advance,
Ashwini
[ August 13, 2008: Message edited by: Bear Bibeault ]
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi Ashwini Kulkarny welcome to Javaranch
Firstly please UseCodeTags.Unformatted code is hard to read and will generate less responses. You can edit your post by clicking the .

why request.getParameter is returning null value?

I think the problem is with the hyperlink, basically clicking a hyperlink does not send form data, you need a submit button instead.

why is the text parameter not showing in the url after it is submitted even though the method is 'GET'?

Simply because its not passed with the request.


Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Yes the link wont act like a form Submit thats why the parameters are not present in the request.. Speaking about STRUTS, are you not defeating the whole point about STRUTS by making calls to request.getParameter() while it already provides the mapping for FORM elements to Form bean properties (which have to be defined in the struts-config.xml)?

Cheers,
Raj.
Ashwini Kulkarny
Greenhorn

Joined: Aug 12, 2008
Posts: 9
Originally posted by Amit Ghorpade:
Hi Ashwini Kulkarny welcome to Javaranch
Firstly please UseCodeTags.Unformatted code is hard to read and will generate less responses. You can edit your post by clicking the .


Simply because its not passed with the request.


Hope this helps
Ashwini Kulkarny
Greenhorn

Joined: Aug 12, 2008
Posts: 9
Originally posted by Amit Ghorpade:
Hi Ashwini Kulkarny welcome to Javaranch
Firstly please UseCodeTags.Unformatted code is hard to read and will generate less responses. You can edit your post by clicking the .


Simply because its not passed with the request.


Hope this helps


Hi Amit,Raj,
Thanks for your reply
Sorry for the bad formatting, actually this is the first time i am using javaranch.. Actually i did try using a submit button instead of a link an wrote a javascript function for the onclick event of the button -
function goForward()
{
document.forms[0].action = "gonext.do";
document.forms[0].submit();
}
However, somehow in this case too the form was not getting submitted and the action class was not getting called. So i decided to try and use a link. It did call the action, but then as you said, the request would not be populated.
Ashwini Kulkarny
Greenhorn

Joined: Aug 12, 2008
Posts: 9
Originally posted by Raj Kamal:
Yes the link wont act like a form Submit thats why the parameters are not present in the request.. Speaking about STRUTS, are you not defeating the whole point about STRUTS by making calls to request.getParameter() while it already provides the mapping for FORM elements to Form bean properties (which have to be defined in the struts-config.xml)?

Cheers,
Raj.


Hi raj,

Actually the thing is that the code I have posted here is just a sample code that i developed just to check if the request object was getting populated or not. The jsp that I am developing for my project consists of tables that are dymanically generated according to a value that I retrieve from session.
For eg, I have a table with 4 columns which are textboxes(qtr1,Qtr2,qtr3,Qtr4) - one for each quarter of the year. Now, depending on a value (effective date) that i get from session, i have to generate tables. So, if effective date is someting like 1/1/2005, I have 4 tables on my jsp (i.e. for 2005,2006,2007,2008(current date)), each with 4 columns for 4 quarters.
Since i dont know how many tables i will have at runtime, i cannot use the form bean.
So what I did was that for each quarter(textbox), i named it according to year and quarter eg- txt2006qtr1, txt2006qtr2 etc..and was hoping to retrieve them in the action class simply by using a for loop.
But unfortunately the javascript function that I had written earlier to call action class onclick of submit button is not working...

I have even tried using a form bean that has a list of 'YearBeans'. and YearBean is a class having 4 properties, one for each qtr, but that also does not work, as the form does not get populated.

Can you suggest anything?

Thanks in advance
Ashwini Kulkarny
Greenhorn

Joined: Aug 12, 2008
Posts: 9
Originally posted by Ashwini Kulkarny:


Hi Amit,Raj,
Thanks for your reply
Sorry for the bad formatting, actually this is the first time i am using javaranch.. Actually i did try using a submit button instead of a link an wrote a javascript function for the onclick event of the button -
function goForward()
{
document.forms[0].action = "gonext.do";
document.forms[0].submit();
}
However, somehow in this case too the form was not getting submitted and the action class was not getting called. So i decided to try and use a link. It did call the action, but then as you said, the request would not be populated.


One more thing, in the javascript function, i tried calling the action in all possible ways -
"/gonext.do"
"/gonext"
"gonext.do"
"gonext"
but it did not work..
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Originally posted by Ashwini Kulkarny:
Sorry for the bad formatting, actually this is the first time i am using javaranch..

NoNeedToSaySorry
Everyone here was a beginner .

function goForward()
{
document.forms[0].action = "gonext.do";
document.forms[0].submit();
}


Well I am not good at Javascript, but you should be using the form name instead of the forms[0], I think.(Its just a suggestion to make your code understandable.)

And is there a need of using Javascript to submit the form? Why dont you let the submit button do it.
By doing so, the action should be invoked and you should see the request query with parameters.

And the correct way to call the action is "/gonext.do".

Hope this helps
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hi,

Try out the suggestion Amit made. I feel that should do it.

They way I understood your problem is that you have 4 years with their corresponding quarter columns. You fetch them from the database and so you know which year is currently referenced and which quarters belong to which years. Why use the actual year values in the form element names? Would not a Year1Quarter1, Year2Quarter2....... suffice? You can then have matching fields in the Form bean too. In case you wont be able to make out which year Year1 refers to why not have a hidden field called Year1 in your form holding that value? Now from the Action you should be able to get what you want done right?

Cheers,
Raj.
Ashwini Kulkarny
Greenhorn

Joined: Aug 12, 2008
Posts: 9
Originally posted by Raj Kamal:
Hi,

Try out the suggestion Amit made. I feel that should do it.

They way I understood your problem is that you have 4 years with their corresponding quarter columns. You fetch them from the database and so you know which year is currently referenced and which quarters belong to which years. Why use the actual year values in the form element names? Would not a Year1Quarter1, Year2Quarter2....... suffice? You can then have matching fields in the Form bean too. In case you wont be able to make out which year Year1 refers to why not have a hidden field called Year1 in your form holding that value? Now from the Action you should be able to get what you want done right?

Cheers,
Raj.


Hi, I had already tried that. It gives me this error - Error 404: com.ibm.ws.webcontainer.servlet.exception.NoTargetForURIException: No target servlet configured for uri: /gonext

No, i do not have 4 years. I have n no of years, and this no will be known at runtime. all i know is tht they have 4 columns. thats y i cant use a form bean directly. i did try using a form bean having a List as its member. eg - YearList. Yearlist was a bean and had 4 properties (1 for each qtr).but that too does not work as the form does not get populated.
Ashwini Kulkarny
Greenhorn

Joined: Aug 12, 2008
Posts: 9
Originally posted by Ashwini Kulkarny:


Hi, I had already tried that. It gives me this error - Error 404: com.ibm.ws.webcontainer.servlet.exception.NoTargetForURIException: No target servlet configured for uri: /gonext

No, i do not have 4 years. I have n no of years, and this no will be known at runtime. all i know is tht they have 4 columns. thats y i cant use a form bean directly. i did try using a form bean having a List as its member. eg - YearList. Yearlist was a bean and had 4 properties (1 for each qtr).but that too does not work as the form does not get populated.


Also, i dont fetch the no of years from DB, i get them frm session..
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Deo Sharma,
Your post was moved to a new topic.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: getParameter gives null value
 
Similar Threads
Struts 1.1 using RAD7
Unable to create a form object
Cannot find bean 'out' in scope request?
Jsp refering to itself
problem getting data from form bean