1
我想開發一個員工管理系統,用戶在數據庫中選擇一個表,myServlet獲取所選按鈕,然後用戶將被重定向到另一個html頁面,以便在該表上選擇操作。 我沒有重定向用戶使用response.sendRedirect(「empActions.html」) 所以任何幫助?如何根據輸入參數將用戶從servlet重定向到我的web項目中的新html文件?
這裏是myFirst html頁面,選擇從數據庫中的表:
<form action="chooseTable" method="post">
<h1>choose a table: </h1>
<input type="button" value="Employees" name="emp">
<input type="button" value="Managers" name="mng">
<input type="button" value="Departments" name="deprt">
</form>
而這裏的FirstMainServelet.java重定向到一個HTML頁面: 包com。示例;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FirstMainServelet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String e = request.getParameter("button");
if(e.equals("Employees"))
{
response.sendRedirect("empActions.html");
System.out.println(" selected action is "+e);
}
else if(e.equals("Managers"))
{
response.sendRedirect("mngActions.html");
System.out.println(" selected action is "+e);
}
else if(e .equals("Departments"))
{
System.out.println(" selected action is "+e);
response.sendRedirect("dptActions.html"); }
}
}
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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MANAGEMENT SYSTEM</display-name>
<servlet>
<description></description>
<display-name>FirstServlet</display-name>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.servlet.FirstMainServelet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/chooseTable </url-pattern>
</servlet-mapping>
</web-app>
首先,不要在redire中使用完全限定的路徑cts –
雖然可能是不好的做法,但完全合格的路徑仍然可以工作。你會得到什麼錯誤信息? –
'response.sendRedirect(「SelectTable.html」)'寫入而不是'response.sendRedirect(「http:127.0.0.1:MYPROJECT/SelectTable.html」)' –