Jump to content
We've recently updated our Privacy Statement, available here ×

Pass parameter from JSP page to JAVA file


2005 IR Help

Recommended Posts

By: cosjav - cosjav

Pass parameter from JSP page to JAVA file

2004-08-05 17:35

Hi,

I know this may be a newbie type question, but I am really stuck...

How can I pass a value from a JSP page to a .java file so that a parameter in the .java file can take on the value passed from the JSP page??

 

Thanks,

 

cosjav

 

 

 

 

 

 

By: bala - bala_eazy

RE: Pass parameter from JSP page to JAVA file

2004-08-05 22:12

Consider

com.sample.SampleCode.java is the java code which need to get values from .jsp page

 

package com.sample

 

public class SampleCode

{

public static String getValue(String str)

{

return str;

}

 

}

 

and this is the .jsp page

----------

sample.jsp

----------

 

<%@ page import="com.sample.SampleCode %>

<%

String s = "Sample";

SampleCode.getValue(s);

%>

 

This is one way of solving your problem.

 

Thanks,

Bala

 

 

 

 

By: Richard Barnett - richard_barnett

RE: Pass parameter from JSP page to JAVA file

2004-08-05 22:26

You misread the question: you're passing a parameter from a Java object to a JSP page.

 

By "parameter" I assume we're talking about a value entered into an HTML form input element.

 

You need to make the JSP render an HTML form & configure the web container so that a Java servlet* handles the form submission. You can then get the parameter value from the servlet request object. I imagine you'll pass this on to the Jasper report or datasource.

 

* Note that the JSP is compiled to a servlet, so for simple cases the HTML form could submit to the JSP that generated it.

 

-- Richard

 

 

 

 

By: cosjav - cosjav

RE: Pass parameter from JSP page to JAVA file

2004-08-06 00:07

Hi Richard,

 

Thanks for your reply. I am not very good with JSP or Servlets yet, is there any good examples (sample codes) that show how its done as you have just explained?

 

 

 

 

By: Chuck Deal - cdeal

RE: Pass parameter from JSP page to JAVA file

2004-08-11 07:57

Have you tried looking at the code that is provided in the Jasper webapp sample? It contains a jsp page that you might be able to learn from.

 

 

 

 

By: bala - bala_eazy

RE: Pass parameter from JSP page to JAVA file

2004-08-06 00:39

hi Richard,

 

plz check this is correct

 

_______________

 

Sample.html

_______________

 

<form name="frm1" action="sample.jsp" >

<input type="hidden" name="emp_name" value="brain"/>

.....

....

....

....

...

</form>

 

.html file passes the value of emp_name to .jsp file.

and .jsp file

 

___________________

 

sample.jsp

___________________

 

<%@ page import="com.sample.SampleCode %>

<%! String or_d;%>

 

<%

or_d = request.getParameter("emp_name");

SampleCode.getValue(or_d);

%>

 

and from the getValue method you can get the value which is pased form

.jsp page and you can send that value as parameter to .jrxml

 

i hope it make sense.

 

Bala

 

 

 

 

 

By: Richard Barnett - richard_barnett

RE: Pass parameter from JSP page to JAVA file

2004-08-06 07:26

Yeah, looks about right.

 

To get closer to the original poster's problem, sample.html would be generated by a(nother) JSP.

 

The Jakarta Struts framework imposes a clear pattern (model-view-controller) on this which encourages discipline in terms of what code goes where -- with traditional JSPs it's too easy to mix request logic with presentation logic.

 

You don't need Struts, you can just stipulate that all form submissions are processed by some servlet, which attaches the results (as beans) to the request & forwards to a JSP -- you then put request logic in the servlet & presentation in the JSP. Struts makes some of this much easier, though.

 

Google "servlet jsp tutorial" (without the quotes) for more info.

 

 

 

 

By: bala - bala_eazy

RE: Pass parameter from JSP page to JAVA file

2004-08-06 23:28

Thanks Richard

 

 

 

 

 

 

By: cosjav - cosjav

RE: Pass parameter from JSP page to JAVA file

2004-08-08 16:55

Hi,

 

Thanks for all your help people...

 

Bala_eazy, I can understand the code you posted (its very helpful, thankyou), but when you say: "you can send that value as parameter to .jrxml" can you please post up an example code that does this?

 

I know I sound stupid, but I have never done this before and one example should get me going...

 

Once again, thanks for all your help.

 

cosjav

 

 

 

 

By: bala - bala_eazy

RE: Pass parameter from JSP page to JAVA file

2004-08-09 07:49

----------

.jrxml

----------

<?xml version="1.0"?>

<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd" [

<!ENTITY reportFonts SYSTEM "./ReportFonts.ent">

]>

 

<jasperReport

name="QueryReport"

pageWidth="595"

pageHeight="842"

columnWidth="515"

columnSpacing="0"

leftMargin="40"

rightMargin="40"

topMargin="50"

bottomMargin="50"

whenNoDataType="AllSectionsNoDetail"

isTitleNewPage="false"

isSummaryNewPage="false">

<!--

This is the way we pass parameter to .jrxml

-->

<parameter name="order" class="java.lang.String"/>

<!-- and the parameter value is used here -->

<queryString><![CDATA[sELECT * FROM por_report ORDER BY $P!{order}]]></queryString>

 

<field name="amt" class="java.lang.Integer"/>

 

<field name="custname" class="java.lang.String"/>

 

<field name="deliverydt" class="java.util.Date"/>

 

<field name="model" class="java.lang.String"/>

 

<field name="place" class="java.lang.String"/>

 

<field name="pordate" class="java.util.Date"/>

 

<field name="por_no" class="java.lang.Integer"/>

 

<field name="qty" class="java.lang.Integer"/>

 

<title>

<band height="70">

<staticText>

<reportElement positionType="Float" x="180" y="36" width="145" height="25" forecolor="#ff8000"/>

<textElement textAlignment="Center">

<font fontName="Arial Black" size="14" isBold="true"/>

</textElement>

<text><![CDATA[sample Reports]]></text>

</staticText>

<line>

<reportElement positionType="Float" x="0" y="24" width="553" height="1"/>

<graphicElement/>

</line>

</band>

</title>

<pageHeader>

<band height="90">

<rectangle>

<reportElement positionType="Float" x="12" y="72" width="529" height="13" backcolor="#808080"/>

<graphicElement/>

</rectangle>

<staticText>

<reportElement positionType="Float" x="24" y="72" width="37" height="13"/>

<textElement>

<font size="8" isBold="true"/>

</textElement>

<text><![CDATA[por_no]]></text>

</staticText>

<staticText>

<reportElement positionType="Float" x="72" y="72" width="61" height="13"/>

<textElement>

<font size="8" isBold="true"/>

</textElement>

<text><![CDATA[cust_name]]></text>

</staticText>

<staticText>

<reportElement positionType="Float" x="144" y="72" width="61" height="13"/>

<textElement>

<font size="8" isBold="true"/>

</textElement>

<text><![CDATA[Plase]]></text>

</staticText>

<staticText>

<reportElement positionType="Float" x="216" y="72" width="85" height="13"/>

<textElement>

<font size="8" isBold="true"/>

</textElement>

<text><![CDATA[por_date]]></text>

</staticText>

<staticText>

<reportElement positionType="Float" x="312" y="72" width="49" height="13"/>

<textElement>

<font size="8" isBold="true"/>

</textElement>

<text><![CDATA[model]]></text>

</staticText>

<staticText>

<reportElement positionType="Float" x="372" y="72" width="25" height="13"/>

<textElement>

<font size="8" isBold="true"/>

</textElement>

<text><![CDATA[qty]]></text>

</staticText>

<staticText>

<reportElement positionType="Float" x="408" y="72" width="61" height="13"/>

<textElement>

<font size="8" isBold="true"/>

</textElement>

<text><![CDATA[deliv_date]]></text>

</staticText>

<staticText>

<reportElement positionType="Float" x="480" y="72" width="49" height="13"/>

<textElement>

<font size="8" isBold="true"/>

</textElement>

<text><![CDATA[amt]]></text>

</staticText>

</band>

</pageHeader>

 

<detail>

<band height="20">

<textField>

<reportElement positionType="Float" x="24" y="5" width="37" height="13"/>

<textElement>

<font size="8" isBold="false"/>

</textElement>

<textFieldExpression class="java.lang.Integer"><![CDATA[$F{por_no}]]></textFieldExpression>

</textField>

<textField>

<reportElement positionType="Float" x="72" y="5" width="61" height="13"/>

<textElement/>

<textFieldExpression class="java.lang.String"><![CDATA[$F{custname}]]></textFieldExpression>

</textField>

<textField>

<reportElement positionType="Float" x="144" y="5" width="61" height="13"/>

<textElement/>

<textFieldExpression class="java.lang.String"><![CDATA[$F{place}]]></textFieldExpression>

</textField>

<textField>

<reportElement positionType="Float" x="216" y="5" width="85" height="13"/>

<textElement/>

<textFieldExpression class="java.util.Date"><![CDATA[$F{pordate}]]></textFieldExpression>

</textField>

<textField>

<reportElement positionType="Float" x="312" y="5" width="49" height="13"/>

<textElement/>

<textFieldExpression class="java.lang.String"><![CDATA[$F{model}]]></textFieldExpression>

</textField>

<textField>

<reportElement positionType="Float" x="372" y="5" width="25" height="13"/>

<textElement/>

<textFieldExpression class="java.lang.Integer"><![CDATA[$F{qty}]]></textFieldExpression>

</textField>

<textField>

<reportElement positionType="Float" x="408" y="5" width="61" height="13"/>

<textElement/>

<textFieldExpression class="java.util.Date"><![CDATA[$F{deliverydt}]]></textFieldExpression>

</textField>

<textField>

<reportElement positionType="Float" x="480" y="5" width="49" height="13"/>

<textElement/>

<textFieldExpression class="java.lang.Integer"><![CDATA[$F{amt}]]></textFieldExpression>

</textField>

<line>

<reportElement positionType="Float" x="0" y="19" width="515" height="1" forecolor="#808080"/>

<graphicElement/>

</line>

</band>

</detail>

</jasperReport>

 

i hope you will understant this

 

Thanks

Bala

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...