如何在两个或多个jsp页面中检索选择的选项标记值

时间:2015-10-06 05:19:25

标签: javascript java jquery jsp servlets

purchase.jsp

Public Shared Function GetChildren(objTree As TreeView) As List(Of TreeNode)
    Dim nodes As List(Of TreeNode) = New List(Of TreeNode)
    For Each parentNode As TreeNode In objTree.Nodes
        nodes.Add(parentNode)
        GetAllChildren(parentNode, nodes)
    Next

    Return nodes
End Function

Public Shared Sub GetAllChildren(parentNode As TreeNode, nodes As List(Of TreeNode))
    For Each childNode As TreeNode In parentNode.Nodes
        nodes.Add(childNode)
        GetAllChildren(childNode, nodes)
    Next
End Sub

view.jsp的

 <form action="view.jsp" method="post">
    <select id='category'>
    <option value=""> Make a selection </option>
    <option value='company'>company</option>
    <option value='institution'>institution</option>
    <option value='hospital'>hospital</option>
    <option value='Others'>Others</option>
    </select>
    <input type = "submit" value="Submit"
    </form>

首先,我将从 <script type="text/javascript"> function setAction(nPage){ document.forms[0].action = nPage; } </script> <form> <select onchange="setAction(this.value)"> <option value=''> Make a selection </option> <option value='PDF2.jsp'> PDF</option> <option value='XLS2.jsp'> XLS </option> <option value='DOC.jsp'> DOC </option> <option value='XLSX2.jsp'> XLSX </option> </select> <input type="submit" value="Submit"> </form> 选择公司,然后点击提交,它将转到purchase.jsp,我将再次选择{ {1}},然后点击提交,它将转到view.jsp页面。现在我的问题是如何打印我在PDF2.jsp中选择的公司PDF2.jsp。所以这里有3个jsp页面,第一个jsp页面中选择的值应该打印在第三个jsp页面中,但序列应该从第一个jsp(purchase.jsp)到第二个jsp(PDF2.jsp)和第二个jsp(purchase.jsp)到第三个jsp(view.jsp)。

PDF2.jsp

view.jsp

2 个答案:

答案 0 :(得分:1)

在view.jsp中,将其设置为像这样的会话

<%
session.setAttribute("category",category);
%>

你可以在任何其他类似的jsp中检索它

String company= (String)session.getAttribute("category");

答案 1 :(得分:0)

purchase.jsp

<form action="view.jsp" method="post">
<select name="category">
<option value=""> Make a selection </option>
<option value="company">company</option>
<option value="institution">institution</option>
<option value="hospital">hospital</option>
<option value="Others">Others</option>
</select>

<input type="submit" value="Submit"/>

</form>

view.jsp的

<script type="text/javascript">

    function setAction(nPage){

        document.forms[0].action = nPage;
    }
</script>
<form>
<% 
String category=request.getParameter("category");
session.setAttribute("cat",category);
%>
<select onchange="setAction(this.value)">
<option value=''> Make a selection </option>
<option value='PDF2.jsp'> PDF</option>
<option value='XLS2.jsp'> XLS </option>
<option value='DOC.jsp'> DOC </option>
<option value='XLSX2.jsp'> XLSX </option>
</select>
<br/>

<input type="submit" value="Submit">
</form>

PDF2.jsp

<body>
<% 
Connection conn = null;

try 
{ 
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/marketing_database","root","root");
    String jrxmlFile ="D:/dev/tools/jasper files/report10.jrxml";
    InputStream input = new FileInputStream(new File(jrxmlFile));
    JasperDesign jasperDesign = JRXmlLoader.load(input);

    System.out.println("Compiling Report Designs");
    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

    System.out.println("Creating JasperPrint Object");
    Map parameters = new HashMap();
    parameters.put("category",session.getAttribute("cat"));
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,conn);
    byte bytes[] = new byte[10000]; 
    JRPdfExporter exporter = new JRPdfExporter();
    ByteArrayOutputStream PDFStream = new ByteArrayOutputStream(); 
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, PDFStream); 
    exporter.exportReport(); 
    System.out.println("Size of byte array:"+PDFStream.size()); 
    bytes = PDFStream.toByteArray(); 
    response.setContentType("application/pdf"); 
    System.out.println("After JasperPrint = 1"); 
    response.setContentLength(bytes.length); 
    System.out.println("After JasperPrint = 2"); 
    PDFStream.close(); 
    System.out.println("After JasperPrint = 3"); 

    OutputStream outputStream = response.getOutputStream(); 
    System.out.println("After JasperPrint = 4"); 
    outputStream.write(bytes, 0, bytes.length); 
    outputStream.flush(); 
    outputStream.close(); 

}
catch(Exception e) 
{e.printStackTrace();} 

%>
</body>