简体   繁体   中英

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.

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. 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

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. 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

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

serialize this pr object in any persistent storage Like File and De-serialize the object in 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

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. For Multiple parameters use & notation.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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