从java main方法调用servlet [英] Invoking servlet from java main method

查看:220
本文介绍了从java main方法调用servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import java.net。*; 
import java.io. *;
公共类样本
{
public static void main(String args [])
{
String line;
try
{
URL url = new URL(http:// localhost:8080 / WeighPro / CommPortSample);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
line = in.readLine();
System.out.println(line);
in.close();
}
catch(例外e)
{
System.out.println(Hello Project ::+ e.getMessage());
}
}
}

我的Servlet正在调用另一个Jsp如下所示的页面,

  RequestDispatcher rd = request.getRequestDispatcher(index.jsp); 
rd.forward(请求,回复);

我没有在浏览器中得到任何反应/输出,一旦servlet必须执行它被调用。



我是否遗漏了这个过程的任何基本步骤?请帮忙!!!

解决方案

如果你想在浏览器中打开它,试试这个



<$ p 。$ p> java.awt.Desktop.getDesktop()浏览(java.net.URI.create( HTTP://本地主机:8080 / WeighPro / CommPortSample));


import java.net.*; 
import java.io.*; 
public class sample
{  
    public static void main (String args[]) 
    { 
        String line;
        try 
        { 
            URL url = new URL( "http://localhost:8080/WeighPro/CommPortSample" ); 
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
            line = in.readLine(); 
            System.out.println( line ); 
            in.close(); 
        }
        catch (Exception e)
        { 
            System.out.println("Hello Project::"+e.getMessage());
        } 
    } 
}

My Servlet is invoking another Jsp page like the below,

 RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
 rd.forward(request, response);

I am not getting any reaction/output in the browser, where the servlet has to be executed once it is invoked.

Am I missing any basic step for this process? Please Help!!!

解决方案

If you want to open it in browser try this

java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://localhost:8080/WeighPro/CommPortSample"));

这篇关于从java main方法调用servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆