使用我的servlet进行Url映射? [英] Url mapping with my servlet?

查看:130
本文介绍了使用我的servlet进行Url映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GWT和GAE。当用户进入以下任何网址时,我想像往常一样为他们提供我的应用程序: www.mysite.com/
http://www.mysite.com/dog
http://www.mysite.com/cat

默认情况下,第一种情况是有效的。我不知道如何让/狗和/猫箱子工作。我想我必须使用url映射修改某些内容才能在web.xml中工作。本质上,我试图让我的应用程序与任何输入的URL一起提供: *

我正在尝试使用一个全新的项目,所以我的web.xml如下所示:

 <! -  Servlets  - > 
< servlet>
< servlet-name> greetServlet< / servlet-name>
< servlet-class> com.me.test.server.GreetingServiceImpl< / servlet-class>
< / servlet>

< servlet-mapping>
< servlet-name> greetServlet< / servlet-name>
< url-pattern> / test / greet< / url-pattern>
< / servlet-mapping>

现在我附加了以下内容:

 < servlet> 
< servlet-name> servletGate< / servlet-name>
< servlet-class> com.me.test.server.ServletGate< / servlet-class>
< / servlet>

< servlet-mapping>
< servlet-name> servletGate< / servlet-name>
< url-pattern> / *< / url-pattern>
< / servlet-mapping>

当我输入网址时:

  http:// localhost:8888 / 
http:// localhost:8888 / dog

我在这里得到了doGet()行上抛出的空指针异常:

  getServletConfig ().getServletContext()的getRequestDispatcher( 的test.html)向前(请求,响应)。。 

我错过了什么?

谢谢

解决方案

您可以发布您当前的web.xml吗?您应该可以添加一个servlet映射,如:

 < servlet> 
< servlet-name> myServlet< / servlet-name>
< servlet-class> com.example.MyServlet< / servlet-class>
< / servlet>

< servlet-mapping>
< servlet-name> myServlet< / servlet-name>
< url-pattern> / *< / url-pattern>
< / servlet-mapping>

然后您可以编写一个简单的Servlet:

  public class MyServlet extends HttpServlet {

public void doGet(HttpServletRequest请求,HttpServletResponse响应)
throws ServletException {
$ b $ getServletConfig()。getServletContext()。getRequestDispatcher(
/MyApplication.html\").forward(request,response);

}

}

或者你可以也可以直接在servlet或JSP中动态生成内容,或者使用任何你喜欢的技术 - 这通常非常有用!



为了响应您的评论:



因为您想要将每条路径映射到servlet,您会得到一个无限循环。 InputStream的解决方案如下所示:

  public class SomeServlet扩展HttpServlet {

@Override
protected void doGet(final HttpServletRequest request,final HttpServletResponse response)
throws ServletException,
IOException {

$ InputStream is = getClass()。getClassLoader()。
getResourceAsStream(/ com / example / MyApplication.html);
最终字节[]缓冲区=新字节[255];
int len = 0; ((len = is.read(buffer))!= -1){
response.getOutputStream()。write(buffer,0,len);
}
response.flushBuffer();


$ b

将MyApplication.html文件放入您的源文件夹在com.example包中。



如果您不想将html文件放在源文件夹中,也可以使用其他方式创建一个InputStream(例如,使用FileInputStream从任何文件中获取)。

I'm using GWT with GAE. When the user enters any of the following urls, I want to just serve my app as usual to them:

http://www.mysite.com/
http://www.mysite.com/dog
http://www.mysite.com/cat

the first case works by default. I'm not sure how to get the /dog and /cat cases to work. I think I have to modify something with the url mappings to get that to work in web.xml. Essentially I'm trying to just get my app served with any url entered:

http://www.mysite.com/*

I'm trying this with a brand new project, so my web.xml looks like this:

<!-- Servlets -->
<servlet>
  <servlet-name>greetServlet</servlet-name>
  <servlet-class>com.me.test.server.GreetingServiceImpl</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>greetServlet</servlet-name>
  <url-pattern>/test/greet</url-pattern>
</servlet-mapping>

and now I've appended the following below:

<servlet>
  <servlet-name>servletGate</servlet-name>
  <servlet-class>com.me.test.server.ServletGate</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>servletGate</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

when I enter a url like:

http://localhost:8888/
http://localhost:8888/dog

I get a null pointer exception thrown on the doGet() line here:

getServletConfig().getServletContext().getRequestDispatcher("test.html").forward(request,response);

what have I missed?

Thanks

解决方案

Can you please post your current web.xml? You should be able to add a servlet mapping like:

  <servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>com.example.MyServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

You would then write a simple Servlet like:

public class MyServlet extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException {

      getServletConfig().getServletContext().getRequestDispatcher(
        "/MyApplication.html").forward(request,response);

  }

}

Or you could also generate the content dynamically, either directly in the servlet, or in a JSP, or by using any technique you prefer - this is often very useful anyway!

In response to your comment:

Because you want to map every path to the servlet, you get an endless loop. A solution with an InputStream could look like this:

public class SomeServlet extends HttpServlet {

    @Override
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
            throws ServletException,
            IOException {

        final InputStream is = getClass().getClassLoader().
                 getResourceAsStream("/com/example/MyApplication.html");
        final byte[] buffer = new byte[255];
        int len = 0;
        while ((len = is.read(buffer)) != -1) {
            response.getOutputStream().write(buffer, 0, len);
        }
        response.flushBuffer();

    }
}

Put the MyApplication.html file in your source folder in the package com.example.

If you don't want to put the html file in your source folder, you can also use any other means to create an InputStream (e.g. from any File using FileInputStream).

这篇关于使用我的servlet进行Url映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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