撰寫Servlet一定要記得在web.xml設定Servlet的mapping、Name等資訊,如下
< servlet>
< servlet-name>HelloServlet</ servlet-name>
< servlet-class>com.demo.WebDemo1.HelloServlet
</ servlet>
< servlet-mapping>
< servlet-name>HelloServlet</ servlet-name>
< url-pattern>/hello.get</ url-pattern>
</ servlet-mapping>
但專案不大可能只有一支Servlet,因為是Java新手,查了許多資料發現網路上也是不少錯誤資訊@@ 紀錄一下方便日後查找
多個Servlet就直接額外新增多組servlet和servlet-mapping的設定即可
< servlet>
< servlet-name>HelloServlet</ servlet-name>
< servlet-class>com.demo.WebDemo1.HelloServlet
</ servlet>
< servlet-mapping>
< servlet-name>HelloServlet</ servlet-name>
< url-pattern>/hello.get</ url-pattern>
</ servlet-mapping>
< !-- Another(i use Jersey) -->
< servlet>
< servlet-name>Jersey REST Service</ servlet-name>
< servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</ servlet-class>
< init-param>
< param-name>com.sun.jersey.config.property.packages</ param-name>
< param-value>com.demo.WebDemo1</ param-value>
</ init-param>
< load-on-startup>1</ load-on-startup>
</ servlet>
< servlet-mapping>
< servlet-name>Jersey REST Service</ servlet-name>
< url-pattern>/rest/*</ url-pattern>
</ servlet-mapping>
沒有留言:
張貼留言