1

我是 JSP 的新手,我需要一些帮助。

我有一个 index.jsp 文件,显然如果你输入“localhost//”,默认情况下它会自动调用 index.jsp。我想知道这是如何工作的,因为我计划在加载 index.jsp 之前先调用一个控制器。

我试图解决它。在我的 index.jsp 中,我放了如下内容:

 if(request.getParameter("submit") == null && 
    request.getAttribute("submit") == null){
    response.sendRedirect("getInformation"); 
 }

在这里,我强制 index.jsp 直接调用我的控制器/servlet。(我在我想调用的控制器上使用了@WebServlet("/getInformation")。

我想知道是否有更好的方法来做到这一点,因为我希望我的控制器/servlet 上的所有逻辑代码和 .jsp 中的所有 html 代码尽可能多。

4

1 回答 1

1

IMO,这是最好的方法。您可以在web.xml. 在下面的示例中,创建一个“愚蠢的”index.html 设置一个 META 标记重定向到您的控制器SomeController(将一些计算从服务器委托给客户端):

web.xml

<welcome-file-list>
  <welcome-file>/WEB-INF/jsp/index.html</welcome-file>
</welcome-file-list>

索引.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="refresh" content="0; URL=./SomeController" />
<title>Some title</title>
</head>
<body>
If you are not automatically redirected please click <a href="./SomeController">here</a>.
</body>
</html>
于 2012-05-09T10:23:20.033 回答