我使用表单获取用户名和密码,并将其与数据库表值进行比较。如果匹配,则应打印用户名和密码。它完成了..
但是,我需要将它们重定向到另一个页面,如果它在web内容文件夹中有效(例如welcome.jsp
)。我该怎么做。我是servlet
的新手,jsp
帮助我。
SaveServlet.java
package myPack;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SaveServlet
*/
public class SaveServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public SaveServlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String u = request.getParameter("username");
String p=request.getParameter("password");
System.out.println("USER-->"+u);
ResultSet rs;
String q="select * from userinfo where username='"+u+"' and password='"+p+"'";
Connection con=null;
String c="jdbc:mysql://localhost:3306/test";
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(c, "root", "MyNewPass");
System.out.println("connection done");
PreparedStatement ps=con.prepareStatement(q);
System.out.println(q);
rs=ps.executeQuery();
System.out.println("done2");
while (rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
}
catch (Exception e) {
// TODO: handle exception
System.out.println("Failed");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
答案 0 :(得分:29)
查看方法
HttpServletResponse.sendRedirect(String Location)
即。用它作为:
HttpServletResponse.sendRedirect("/your/new/location.jsp")
或者,看看方法
HttpServletResponse.setHeader(String name, String value)
通过添加位置标题来设置重定向:
HttpServletResponse.setHeader("Location", "/your/new/location.jsp");
答案 1 :(得分:3)
请使用以下代码告诉我
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(c, "root", "MyNewPass");
System.out.println("connection done");
PreparedStatement ps=con.prepareStatement(q);
System.out.println(q);
rs=ps.executeQuery();
System.out.println("done2");
while (rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
response.sendRedirect("myfolder/welcome.jsp"); // wherever you wanna redirect this page.
}
catch (Exception e) {
// TODO: handle exception
System.out.println("Failed");
}
myfolder/welcome.jsp
是您jsp
页面的相对路径。因此,请根据您的jsp
页面路径进行更改。
答案 2 :(得分:2)
String u = request.getParameter("username");
String p = request.getParameter("password");
try {
st = con.createStatement();
String sql;
sql = "SELECT * FROM TableName where USERNAME = '" + u + "' and PASSWORD = '"
+ p + "'";
ResultSet rs = st.executeQuery(sql);
if (rs.next()) {
RequestDispatcher requestDispatcher = request
.getRequestDispatcher("/home.jsp");
requestDispatcher.forward(request, response);
} else {
RequestDispatcher requestDispatcher = request
.getRequestDispatcher("/invalidLogin.jsp");
requestDispatcher.forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
rs.close();
ps.close();
con.close();
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}