• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getParameter gives null value

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deo Sharma,
Your post was moved to a new topic.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic