简体   繁体   English

从Java类调用servlet

[英]calling a servlet from a java class

I have got an issue in calling a servlet within a simple java class for testing purpose. 在测试简单Java类中调用servlet时遇到问题。 I want to pass a parameter along with the servlet and the method will be POST. 我想将参数与Servlet一起传递,该方法将为POST。 How it can be achieved? 如何实现?

While searching through the answers I saw someone recommended HTTPClient. 在搜索答案时,我看到有人推荐了HTTPClient。 But just wondering whether there is a way to avoid this. 但是,只是想知道是否有避免这种情况的方法。

All you have to do is to request a URL. 您要做的就是请求一个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: Java程序:

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. 将servlet .class文件放在servlet引擎的WEB-INF/classes目录中。 Run the other class on command line the usual way: 按常规方式在命令行上运行另一个类:

java CmdLineApplication

You should see the string from the servlet 您应该看到servlet中的字符串

The servlet container is responsible for initializing and calling servlet. Servlet容器负责初始化和调用Servlet。 You cannot do directly calling a servlet class. 您不能直接调用servlet类。

For posting a request, you can use HttpClient. 要发布请求,可以使用HttpClient。

Apache HTTPClient Apache HTTP客户端

Or you can use a 或者您可以使用

URLConnection 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);
}

%>

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

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