2014-02-18 67 views
0

我是servlets的新手,我看到有很多代碼解釋瞭如何通過servlet的請求響應來完成一個完整的道路,但大多數情況下他們使用的是response.getWritter().println("something"),但是,我已經看到,還有另外一種生成html內容的方法,比如當servlet被訪問時應該默認收取的索引頁。我有一個servlet和web.xml的基本示例,我想知道你是否可以幫助我理解我能做些什麼來使index.html在我輸入時顯示localhost:8280/persistence-with-jdbc2/...如何訪問索引頁?

這是基本的servlet的:

@WebServlet(urlPatterns = "/PersistenceWithJDBCServlet2") 

public class PersistenceWithJDBCServlet2 extends HttpServlet { 

    private static final long serialVersionUID = 1L; 

    private static final Logger LOGGER = 
     LoggerFactory.getLogger(PersistenceWithJDBCServlet2.class); 

    private PersonDAO personDAO; 


    @Override 
    public void init() throws ServletException { 

     System.out.println("init"); 
    } 

    @Override 
    protected void doGet(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, IOException { 
     //What can I use here? 
    } 

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

     try { 
      // test code 
     } catch (Exception e) { 
      response.getWriter().println(
       "Persistence operation failed with reason: " 
        + e.getMessage()); 
      LOGGER.error("Persistence operation failed", e); 
     } 
    } 
} 

和web.xml文件的內容:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web- app_3_0.xsd" id="WebApp_ID" version="3.0"> 
<display-name>persistence-with-jdbc2</display-name> 
<welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
</welcome-file-list> 
    <servlet> 
    <description></description> 
    <display-name>PersistenceWithJDBCServlet2</display-name> 
     <servlet-name>PersistenceWithJDBCServlet2</servlet-name> 
    <servlet-class>com.sap.cloud.sample.persistence.PersistenceWithJDBCServlet2</servlet-class> 
    </servlet> 
<servlet-mapping> 
    <servlet-name>PersistenceWithJDBCServlet2</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
<resource-ref> 
    <res-ref-name>jdbc/DefaultDB</res-ref-name> 
<res-type>javax.sql.DataSource</res-type> 
</resource-ref> 
</web-app> 

感謝您的時間!

回答

1

您可以將其重定向到您想要的任何webadress。

@Override 
protected void doGet(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, IOException { 
     response.sendRedirect("/yourwebAdress/index.html"); 

} 
+0

我沒有工作,我不知道爲什麼,說太多的重定向... – kar

0

我的事情你創建喜歡的index.jsp索引頁,並把這個下面的示例代碼等爲:

<body> 
<jsp:forward page="/UserController?action=listUser" /> 
</body> 

並稱之爲索引頁在web.xml頁面,像

<display-name>Simple1</display-name> 

    <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 

    <servlet> 
    <description></description> 
    <display-name>UserController</display-name> 
    <servlet-name>UserController</servlet-name> 
    <servlet-class>com.pro3.controller.UserController</servlet-class> 
    </servlet> 

並添加這一個在控制器頁:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{ 
    String forward=""; 
    String action = request.getParameter("action");