简体   繁体   English

java-将参数从Java类传递到servlet

[英]java - passing parameter from a java class to a servlet

I have a peculiar business case where I am trying to pass parameters(both variables and arrays) from a java class to a servlet and invoke a servlet from the java class. 我有一个特殊的业务案例,我试图将参数(变量和数组)从Java类传递到Servlet,然后从Java类调用Servlet。

What I did is, just made the parameters global assigned their values inside the java class and then invoked the servlet from the Java class. 我所做的是,只需在java类内部为参数全局分配其值,然后从Java类中调用servlet。 But this doesn't seem to be good idea. 但这似乎不是一个好主意。

Hers is my sample code: 她是我的示例代码:

/* Assigning all the variables and array values inside a java class */

JavaClass.java
...
var1 = value1;
var2 = value2;
arr1 = {val1, val1, val3};
...

I am referring URLCOnnection in Oracle's documentation found here 我在这里的 Oracle文档中引用URLCOnnection

The thing is I m not understanding the way in which we can send the parameters. 问题是我不了解我们发送参数的方式。

URL url = new URL("http://localhost:8080/ProjectCharterApproval2/CharterApprover");

URLConnection connection = url.openConnection() ; 

connection.setDoOutput(true);

/** After this how should I pass parameter to the servlet as an input object or output object ? **/

Any help will highly be appreciated. 任何帮助将不胜感激。

Thanks in advance. 提前致谢。

Try query string to pass vars 尝试查询字符串以传递变量

URL url = new URL("http://localhost:8080/ProjectCharterApproval2/CharterApprover?var1="+var1+"&var2="+var2+"&var3[]="+arr1);

this array approach works fine with php but don't know about JAVA. 这种数组方法可以在php上正常工作,但不了解JAVA。 Please try this. 请尝试这个。

Or if you don't want to use this, try this 或者,如果您不想使用此功能,请尝试以下操作

class Param implements Serializable{
    private String var1;
    private String var2;
    private String []var3;
    public String getVar1() {
        return var1;
    }
    public void setVar1(String var1) {
        this.var1 = var1;
    }
    public String getVar2() {
        return var2;
    }
    public void setVar2(String var2) {
        this.var2 = var2;
    }
    public String[] getVar3() {
        return var3;
    }
    public void setVar3(String[] var3) {
        this.var3 = var3;
    }
    public Demo(String var1, String var2, String[] var3) {
        super();
        this.var1 = var1;
        this.var2 = var2;
        this.var3 = var3;
    }
}

Now in your java class 现在在您的java类中

Param pr=new Param(var1,var2,arr1);

serialize this pr object in any persistent storage Like File and De-serialize the object in servlet. 在任何持久性存储(如文件)中序列化此pr对象,并在servlet中反序列化该对象。

i have searched for the very same question that maybe others have the exact problem, and i found an accepted answer over here. 我搜索了同样的问题,也许其他人也有确切的问题,我在这里找到了可以接受的答案。

Java:how to pass value from class/bean to servlet Java:如何将值从类/ bean传递到servlet

and for the passing parameters and/or arrays is done via that line: 对于传递参数和/或数组的操作是通过该行完成的:

request.setAttribute("areas", areas);

similar to ("key": value) sets of request you are about to do. 类似于(“键”:值)您将要执行的请求集。

regards.. 问候..

In URL itself pass parameters as we pass usually after question mark. 在URL本身中,我们通常在问号之后传递参数。 For Multiple parameters use & notation. 对于多个参数,请使用&表示法。

URL url = new URL("http://127.0.0.1:8080/ProjectCharterApproval2/CharterApprover?parameter=" + param );

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

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