如何解决在java RequestDispatcher方法

时间:2016-01-23 01:31:43

标签: java jsp servlets

我正在做一个基于java RMI网络的程序,它由Registration_interface.java(java接口),Registration_server.java(java类)和带有login.jsp(servlet)的login.java组成,但是服务器端有一个

的问题
if (rs.next()) {
   RequestDispatcher rd =request.getRequestDispatcher("register.jsp");
   rd.forward(request, response);
}

可以帮我解决这个问题:

Registration_interface.java(interace side)

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Registration_interface extends Remote {

    public String login(String User, String Pass) throws RemoteException;

}

Registration_server.java(服务器端)

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.sql.*;
import javax.servlet.RequestDispatcher;

public class Registration_server extends UnicastRemoteObject implements Registration_interface {

    public Registration_server() throws RemoteException {

        System.out.println("server initialized.....");
    }

    public String login(String User, String Pass) throws RemoteException {
        Connection conn = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/users", "root", "2200");
            String sql = "SELECT * FROM user where UserName=('" + User + "') and Password=('" + Pass + "')";
            Statement st = conn.createStatement();

            ResultSet rs = st.executeQuery(sql);
            if (rs.next()) {
                RequestDispatcher rd = request.getRequestDispatcher("register.jsp");
                rd.forward(request, response);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Sucess";

    }

    public static void main(String[] args) {
        Registration_server server = null;
        try {
            server = new Registration_server();
            Registry registry = LocateRegistry.createRegistry(127);// default:1099
            registry.rebind("RemoteObject1", server);
            System.out.println("Server Ready..........");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

login.java(servlet-client side)

import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/login"})
public class login extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        try {
            Registration_interface obj = null;
            try {

                obj = (Registration_interface) (Naming.lookup("//localhost:127/RemoteObject1"));
            } catch (NotBoundException e) {
                e.printStackTrace();
            }

            String User = request.getParameter("User").toString();
            String Pass = request.getParameter("Pass").toString();

            String a = obj.login(User, Pass);

            out.println("<u><b> You completed!!!!!!!!!!!!!!</b></u>" + a);
            //out.println("<include>register.jsp<include>");
        } finally {

            out.close();

        }
    }

    private void processRequest(HttpServletRequest request, HttpServletResponse response) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

login.jsp(与serlet集成)

&#13;
&#13;
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Page</title>
    </head>
    <body>
        <form action="login" method="post">
            <table>
                <tr>
                    <th>UserName</th>
                    <td><input type="text" name="User"</td>
                </tr>
                <tr>
                    <th>Password</th>
                    <td><input type="password" name="Pass"</td>
                </tr>
                <tr>
                    <th></th>
                    <td><input type="submit" name="login" value="Login"</td>
                </tr>
            </table>
        </form>
    </body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

将Registration_server.java中的服务对象声明为HttpServletRequest请求和HttpServletResponse res;

答案 1 :(得分:0)

您不能使用未运行servlet容器的服务器中的requestRequestDispatcher对象。只需删除此代码

即可
RequestDispatcher rd = request.getRequestDispatcher("register.jsp");
rd.forward(request, response);