2016-01-23 87 views
-1

上午符號提示做一個Java RMI基於網絡的程序,由Registration_interface.java(Java接口)的,Registration_server.java(Java類)和login.java與login.jsp(servlet的),但服務器側有一個問題,如何解決無法找到的Java方法的RequestDispatcher

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

可以在任何幫助我:

Registration_interface.java(interace側)

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的客戶端)

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 integtated)

<%@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>
在Registration_server.java

+1

你將不得不從編譯器複製/粘貼實際的錯誤信息,並指示在代碼信息涉及的線路。 –

回答

0

聲明服務對象作爲HttpServletRequest的請求和HttpServletResponse RES;

0

您不能使用未運行servlet容器的服務器中的requestRequestDispatcher對象。只是刪除此代碼

RequestDispatcher rd = request.getRequestDispatcher("register.jsp"); 
rd.forward(request, response); 
相關問題