我是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>
感謝您的時間!
我沒有工作,我不知道爲什麼,說太多的重定向... – kar