本帖最后由 ergherh 于 2010-07-01 14:40:44 编辑

解决方案 »

  1.   

    servlet里参数直接有,普通类不行
      

  2.   

    我是在servlet的init() {}里调用的普通java类。而你说的public void service(ServletRequest request, ServletResponse responise){}在service方法里。请问我如何在init内获得request???
    然后取出request.getHeader("HOST")再传到普通java类里
      

  3.   

    首先在你的普通类你导入request和response
    然后再普通类的方法的第一和第二个参数分别为 request,response;这样,在你的普通类里就可以使用request和response对象了,并可以使用它们的方法。
    书写方法可参见servlet里的写法。
    来分啊,呵呵
      

  4.   

    你就是那个想通过servlet获取路径的人吧。看了你刚刚那个帖子了,你可以把路径作为String传过去啊。
      

  5.   

    哎,说了我是在init里调用的普通类,我的问题就是问如何才能在servlet的init内获得request对象???
      

  6.   

    web.xml
      <servlet>
        <servlet-name>...</servlet-name>
        <servlet-class>...</servlet-class>
          <init-param>
          <param-name>Url</param-name>
          <param-value>http://www.domin.com</param-value>
         </init-param>
      </servlet>servlet init();String domain_name = this.getServletConfig().getInitParameter("Url");
    Test test = new Test(domain_name);Test.java private String domain_name = "";
    public Test(String domain)
    {
      this.domain_name = domain;
    }
    public conUrl()
    {
    String url = this.domain_name + "/xxx/aaa.jsp";
    URL dU= new URL(url); 
    InputStream in = dU.openStream()
    }
      

  7.   


    那我理解错了,Init不可能得到request对象的。
      

  8.   


    用人家这个吧,Init里面连servletContext都还是Null呢。只能找参数去。
      

  9.   

    init方法是服务器获得第一个到该servlet请求时候调用的,仅调用一次,你后面每次的请求都不会调用这个方法。请问你怎么获得request...
      

  10.   

    WebContext ct=WebContextFactory.get();
    ct.getHttpServletRequest()
    LZ 试试
      

  11.   

    WebContext 用这个需要导入什么包 ?
      

  12.   


    写错地方了,init可以的,我试试。
      

  13.   

    getContextPath()能得到你的工程名,剩下的自己拼可以么?
      

  14.   

    public void init() throws ServletException {
    // Put your code here
    HttpServletRequest request;
    }不能直接这样写吗、Servlet里不能用HttpServletRequest对象?搞什么?
      

  15.   

    getContextPath()得到的是文件的物理路径,而不是类似于localhost:8888/或http://www.xxx.com/这样的url路径,我是想得到jsp文件的执行结果,而不是文件本身的code内容
      

  16.   


    不会吧,this.getServletContext().getContextPath() 得到的是工程名吧。
      

  17.   

    啊,我也发愁如何获得HttpServletRequest ………………………………