关于欢迎页面和访问Web服务方法 [英] About Welcome Page and Accessing Web Service Methods

查看:126
本文介绍了关于欢迎页面和访问Web服务方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到配置问题(或基本了解后台工作原理)。我通过检查Web应用程序和ReSt api复选框创建了一个JAVAEE项目(在intellij中使用glassfish 5.0)。我有下面的示例代码,Web方法可以工作,但欢迎页面不起作用。我的web.xml和示例Web服务方法如下所示。

I have a problem with configuration ( or basic understanding how things work at background). I create a JAVAEE project by checking Web application and ReSt api checkbox ( in intellij with glassfish 5.0). I have sample code below which web methods work but welcome page does not work. My web.xml and sample web service methods are below.

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>test</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    **<url-pattern>/ *</url-pattern>**
</servlet-mapping>



@Path("/RestTest")
public class TestString {

@Context
ServletContext context;

@GET
@Path("insertdb/{param1}/{param2}")
@Produces(MediaType.APPLICATION_JSON)
public Object writeToDb( @PathParam("param1") String param1
                        ,@PathParam("param2") String param2){
     try{
        String password=  context.getInitParameter("DbPassword");
        Class.forName("org.mariadb.jdbc.Driver");
        Connection dbCon = DriverManager.getConnection(
                 "jdbc:mariadb://xxx/testdb", "root", password);
        PreparedStatement stmt=dbCon.prepareStatement(
                "INSERT INTO TestTable VALUES(?,?)");
        stmt.setString(1,param1);
        stmt.setString(2,param2);
        stmt.executeUpdate();

        dbCon.close();

        return "Success";
    }catch(SQLException | ClassNotFoundException ex){
        return  ex.toString();
    }

}

@GET
@Path("sum/{sum1}/{sum2}")
@Produces(MediaType.TEXT_HTML)
public String calculateSum(@PathParam("sum1") int param1
        ,@PathParam("sum2") int param2){
    return ""+(param1 + param2);
}

如果我更改此行 url-pattern / *to/
然后欢迎页面可访问但不是方法。

If i change this line url-pattern "/*" to "/" then welcome page is accessible but not methods.

因此我想要的是,有一个欢迎页面,我将用于我的Web服务的文档(我不想要SOAP)和Web方法必须通过添加/到基本URL来工作。我如何实现这一点以及/ *和/

Thus what i want is, having a welcome page which i will use for documentation for my web services(i dont want SOAP) and web methods must work by adding / to base url. How can i achieve that and what is difference between /* and /

推荐答案

要回答您的问题,<$ c $之间的区别c>/和 / *

包含如果没有其他模式匹配,模式/匹配请求。这是默认映射。映射到此模式的servlet称为默认servlet。默认映射通常指向应用程序的第一页。示例:

A mapping that contains the pattern "/" matches a request if no other pattern matches. This is the default mapping. The servlet mapped to this pattern is called the default servlet. The default mapping is often directed to the first page of an application. Example :

两个请求都将显示来自的相同内容index.jsp

Both requests will display same contents from index.jsp

http://myhost.com/index.jsp

http://myhost.com/

现在,包含/ * 的映射会覆盖所有其他servlet,包括servlet容器提供的所有servlet,例如缺省servlet和JSP servlet 。无论你发出什么样的请求,它都会在那个servlet中结束。因此,这对于servlet来说是一个糟糕的URL模式。

Now, a mapping that contains "/*" overrides all other servlets, including all servlets provided by the servlet container such as the default servlet and the JSP servlet. Whatever request you fire, it will end up in that servlet. This is thus a bad URL pattern for servlets.

将网址格式更改为特定格式而非默认格式。

Change the URL pattern to specific instead of default pattern.

<servlet>
    <servlet-name>webservice</servlet-name>  //servlet name
    <servlet-class>com.rest.MyRestServlet</servlet-class>  //servlet class
</servlet>
 <servlet-mapping>
    <servlet-name>webservice</servlet-name>   //servlet name
    <url-pattern>/RestTest/*</url-pattern>  //all webservice request
</servlet-mapping>

所有网络服务请求均可通过

All the web service request are accessible through

http://myhost.com/RestTest/






您可能还有兴趣看看



web.xml中的URL模式是什么以及如何配置servlet

Java基础知识Servlet

这篇关于关于欢迎页面和访问Web服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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