html – 如何从servlet重定向到jsp页面

前端之家收集整理的这篇文章主要介绍了html – 如何从servlet重定向到jsp页面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用表单获取用户名和密码,并将其与数据库表值进行比较。如果匹配,则应打印用户名和密码。完成..

但是,如果在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);
        }

    }

解决方法

看看方法
HttpServletResponse.sendRedirect(String Location)

即将其用作:

HttpServletResponse.sendRedirect("/your/new/location.jsp")

或者,查看方法

HttpServletResponse.setHeader(String name,String value)

重定向是通过添加位置标题来设置的:

HttpServletResponse.setHeader("Location","/your/new/location.jsp");
原文链接:https://www.f2er.com/html/233568.html

猜你在找的HTML相关文章