简体   繁体   中英

calling a servlet from a java class

I have got an issue in calling a servlet within a simple java class for testing purpose. I want to pass a parameter along with the servlet and the method will be POST. How it can be achieved?

While searching through the answers I saw someone recommended HTTPClient. But just wondering whether there is a way to avoid this.

All you have to do is to request a URL. Put all the name, value pairs as you would normally have, and open a stream on it.

Here the example code:

import java.io.*; 
import javax.servlet.http.*; 
import javax.servlet.*;

public class SpecialServlet extends HttpServlet 
{

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{ 
    PrintWriter out = res.getWriter(); 
    out.println ("Hello " + req.getParameter("name") + ", this is SpecialServlet!"); 
    out.close(); 
}
} 

The java program:

import java.net.*; 
import java.io.*; 

public class CmdLineApplication
{
public static void main (String args[])
{
String line;
try
{
URL url = new URL( "http://127.0.0.1/servlet/special?name=CmdLineApplication" );
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
line = in.readLine();
System.out.println( line );
in.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

Place the servlet .class file in the WEB-INF/classes dir of your servlet engine. Run the other class on command line the usual way:

java CmdLineApplication

You should see the string from the servlet

The servlet container is responsible for initializing and calling servlet. You cannot do directly calling a servlet class.

For posting a request, you can use HttpClient.

Apache HTTPClient

Or you can use a

URLConnection

Use following code.Using this code you can maintain sessions also.

package webapp;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;


public class HttpClientSingleton {
    public static HttpClientSingleton mSingleton = null;

    public static HttpClient mHttpClient = null;

    //constructor
    private HttpClientSingleton(){
    mHttpClient = new DefaultHttpClient();
    }

    public static HttpClientSingleton getInstance(){
    if( mSingleton == null ){
        mSingleton = new HttpClientSingleton();
    }
    return mSingleton;
    }



}

jsp file:

<%
try{
    int i=0;
String sid = "ED6379C8FDDB801EB970BBDE55E5732D"; 
HttpClientSingleton h1 = HttpClientSingleton.getInstance();
HttpClient client= h1.mHttpClient;


HttpPost responses = new HttpPost("http://localhost:8080/docinbangalore/Billing_address_display?sid="+sid+"");
try {

    HttpResponse respons = client.execute(responses);
    BufferedReader rd = new BufferedReader(new InputStreamReader(respons.getEntity().getContent()));
    String line = "";
    out.println(line = rd.readLine());
    while ((line = rd.readLine()) != null) {
     out.println(line);

    }
  } catch (IOException e) {
    e.printStackTrace();
  }}
catch(Exception e)
{
    out.println(e);
}

%>

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