简体   繁体   English

如何从JSP页面获取参数到Java Helper函数

[英]How to get parameters from jsp page onto java helper function

I'm trying to get the a parameter from a drop down menu that's on the jsp file, onto my helper function which is in a java file. 我正在尝试从jsp文件上的下拉菜单中获取一个参数,并将其添加到java文件中的helper函数中。 I'm checking to see if the user clicks a button and then I want to take the "rows" parameter. 我正在检查用户是否单击了按钮,然后要使用“行”参数。 This is what the drop down menu looks like ... 这是下拉菜单的样子...

<div class="panel panel-default">
    <div class="panel-body">
        <div class="bottom-nav">
            <h4> Options </h4>
            <form action="AnalysisHelper.java"> 
                    <div>
                Rows: 
                    <select name="rows">
                        <option value="users">users</option>
                        <option value="states">states</option>
                    </select>
                </div>

                    <div>
                    <input type="submit" value="Next 20 Users" name="nextUsers">
                    <input type="submit" value="Next 10 products" name="nextProd">
                    <input type="submit" value="RUN QUERY!" name="run">
                </div>

and this is my helper function: 这是我的辅助函数:

package helpers;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class AnalysisHelper {

    public static List<AnalysisWithCategories> listAnalysis() {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        // list object holding data
        List<AnalysisWithCategories> analysis = new ArrayList<AnalysisWithCategories>();

        try {
            try {

                // connect to DB
                conn = HelperUtils.connect();

            } catch (Exception e) {

                // error
                System.err.println("Internal Server Error. This shouldn't happen.");
                return new ArrayList<AnalysisWithCategories>();
            }

            // get all the products
            stmt = conn.createStatement();

                        // check if user wants to filter results
            String runQuery = request.getParameter("run");
            if()

            // might need indexes on userandstates(u_id), userandproductsales(u_id), userandproductsales(p_id), product(p_id)
            rs = stmt.executeQuery("SELECT s.p_name, s.p_id, s.u_name, s.u_id, s.s_price, s.quantity, s.name, s.c_name, u.u_totalSales, p.p_totalSales" 
                    + " FROM userAndProductSales s" 
                    + " left outer join usersandstates u on u.u_id = s.u_id" 
                    + " join product p on p.p_id = s.p_id"
                    + " ORDER BY u_name, p_name LIMIT 20");

I've tried request.getParameter("run") and request.getParameter("rows") but I get an error saying that "request is not resolved" . 我已经尝试了request.getParameter("run")request.getParameter("rows")但收到一条错误消息,提示“请求未解决” How do I map the request to my jsp file? 如何将请求映射到我的jsp文件? Or if there's another way to get the parameter, can you please show me. 或者,如果还有其他获取参数的方法,可以请告诉我。

It's more than architectural problem. 不仅仅是建筑问题。

What you must remember is that JSP is compiled into HTML and sent to the client - once there (or actually after compiling is done), it has no idea what happens on the server. 您必须记住的是,JSP被编译为HTML并发送到客户端-一旦到达客户端(或实际上在完成编译之后),它就不会知道服务器上会发生什么。 It is no longer JSP and knows nothing of Java. 它不再是JSP,并且对Java一无所知。

If you want to call a function on the server, you need to expose the function (servlet, REST, etc.) and call it via Javascript on the client. 如果要在服务器上调用一个函数,则需要公开该函数(servlet,REST等),并通过客户端上的Javascript对其进行调用。 The client does not (and should not) know the internals of your server - even if you, as the programmer, do. 客户端不(也不应该)知道服务器的内部结构,即使您作为程序员也是如此。

Specifically as to request not being resolved: 具体来说,要求不解决:

You need to write a function which will get as one of its parameters an httprequest, which is injected by the network communication framework (JEE, Spring, another servlet) - upon which you can request parameters which you pass from client (!). 您需要编写一个函数,该函数将通过网络通信框架(JEE,Spring,另一个Servlet)注入httprequest作为其参数之一,在该函数上您可以请求从客户端(!)传递的参数。 It does not happen by magic. 魔术不会发生这种情况。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM