我使用表单获取用户名和密码,并将其与数据库表值进行比较。如果匹配,则应打印用户名和密码。完成..
但是,如果在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,IOException { // TODO Auto-generated method stub doGet(request,response); } }