request.getRequestDispatcher()。forward()无法显示请求的HTML

时间:2019-06-28 15:12:37

标签: servlets forward requestdispatcher

我正在为数据库管理器网络应用制作原型。成功登录后,该应用程序需要导航到数据库管理器视图(dbmanager.html)。但是,请求的页面不会显示在浏览器中。

环境是Windows 10,Apache-Tomcat-7.0.94,Glassfish 4,MySql 5.7.24。

login.html

    <form id="login_form" name="login_form" action="LogIn"
      method="post" onsubmit="formPost(this);return false;">
        <fieldset>
            <!-- more fields here -->
            <input id="input_login" value="Login" type="submit" >
        </fieldset>
        <p id="mesg" style="color: red;"></p>
    </form>

LogIn.java

    @WebServlet(name = "LogIn", urlPatterns = { "/LogIn" })
    @MultipartConfig
    public class LogIn extends HttpServlet {
        protected void doPost(
            HttpServletRequest request,
            HttpServletResponse response
        ) throws ServletException, IOException {

        //get the inputs, customized method getParameter
        String user = getParameter("user");
        String password = getParameter("password");

        //validate inputs
        String errorMsg = null;
        if(user == null || user.equals("")){
            errorMsg ="User can't be null or empty";
        }
        if(password == null || password.equals("")){
            errorMsg = "Password can't be null or empty";
        }

        //show error, if so
        if(errorMsg != null){
            response.setContentLength("#mesg=".length() + errorMsg.length());
            PrintWriter out= response.getWriter();
            out.println("#mesg=" + errorMsg);
            return;
        }


        //no errors, forward / redirect to next page
        request.getRequestDispatcher("/contents/dbmanager.html")
         .forward(request, response);

    }

    protected void doGet(
        HttpServletRequest request,
        HttpServletResponse response
    ) throws ServletException, IOException {
        request.getRequestDispatcher("/contents/dbmanager.html")
            .forward(request, response);
    }

}

JavaScript(无jQuery)

    function formPost(_form){
        var xhr = new XMLHttpRequest();
        xhr.open(_form.method, _form.action);
        xhr.onload = function(event){
            event.preventDefault(); // Important! Prevents submitting the form.
            var response = event.target.response;
            //process response...
            console.log(response);
            //response is a array of string of structure {id=value}
            var idMark = response.indexOf("#");
            if( isNaN(idMark) ){
              return;
            }
            if(idMark !== 0){
              return;
            }
            //other validations here...
        };
        // or onerror, onabort
        var formData = new FormData(_form);
        xhr.send(formData);
    }

1 个答案:

答案 0 :(得分:0)

我在DBManager servlet中设置了以下代码,并更新了JS。

Servlet

@WebServlet(name = "DBManager", urlPatterns = { "/DBManager" })
@MultipartConfig
public class DBManager extends HttpServlet {

   private static final long serialVersionUID = 1L;

   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      System.out.print("DBManager dG conx:");
      Connection conx =
         (Connection) ((HttpSession)LogIn.getHTTPSession()).getAttribute("Connection");
      System.out.println(conx.toString());
   }
}

我正在从创建对象的Connection servlet中提取LogIn对象。

编辑后的JS

function onload(){
    //alert("win.loc:"+window.location);
    var url = window.location;
    var pageName = url;
    if(url===null || pageName===null) {return;}

    url = url.toString();

    //other logic go here

    if(pageName === "dbmanager"){
        processGet(url);
    }

}
function processGet(url){
    alert("processGet url="+url);
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var data = xhr.responseText;
            alert("DBManager xhr.responseText:"+data);
        }
    }
    xhr.open('GET', 'DBManager', true);
    xhr.send(null);
}

System.out中的打印结果类似于...

LogIn dP con=com.mysql.cj.jdbc.ConnectionImpl@1df9967
DBManager dG conx:com.mysql.cj.jdbc.ConnectionImpl@1df9967

为我工作。