1
我使用Apache Tomcat和一個servlet從MySQL數據庫查詢用戶名和密碼正確使用登錄servlet的重定向到URL
HTML網頁代碼:
<form action="./login" method="POST">
User name: <input type="text" name="username" size="20"><br>
Password: <input type="password" name="pwd" size="20">
<br><br>
<input type="submit" value="Submit">
</form>
servlet代碼:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
public class VerifyLogin extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
String n1 = req.getParameter("username");
String n2 = req.getParameter("pwd");
res.setContentType("text/html");
PrintWriter out = res.getWriter();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","paSSword");
PreparedStatement ps = con.prepareStatement("select * from auth WHERE username=? && password=?");
ps.setString(1,n1);
ps.setString(2,n2);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
res.sendRedirect("report.html");
}
else
{
res.sendRedirect("Invalid.html");
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}
web.xml代碼:
<web-app>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>VerifyLogin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
我的目錄結構是
=============>tomcat
+webapps
+myapp
+
+report.html
+report2.html
+xxxx.html
+WEB-INF
+classes
+lib
+web.xml
我是能夠成功地驗證和用戶重定向到所需的頁面(report.hmtl),問題是當用戶複製登錄後的地址說http://192.168.2.2:8080/myapp/Reports.html,並在另一個瀏覽器粘貼地址他不會被要求登錄。
report.html有更多的鏈接,如果有人知道鏈接,可以在不登錄的情況下訪問它。所有的.html頁面都放在WEB-INF文件夾之外如何保護那些來自直接訪問的示例(例如report2.html,xxx.html)?而無需將html頁面轉換爲jsp
thnaks for reply,can not it be done without without html to jsp ?? PS:忘記提及它的問題 – 2012-04-03 14:40:43
不,不能在HTML中檢查會話,並且在轉換爲jsp時沒有問題... – 2012-04-03 14:45:43
我的問題是,我在WEB-INF文件夾外有56個HTML頁面,有沒有什麼辦法我可以自動將它們轉換成批次嗎? – 2012-04-03 15:00:28