简体   繁体   中英

Out.Print method not working?

Here is my code

LoginController.java

package mvc.demo.control;

    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import mvc.demo.model.Authenticate;


    public class LoginController extends HttpServlet {
        private static final long SerialVersionUID=1L;

        public LoginController()
        {
            super();
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            String s1=req.getParameter("username");
            String s2=req.getParameter("password");
            RequestDispatcher rd=null;
            Authenticate au=new Authenticate();
            String result=au.authorise(s1, s2);
            if(result.equals("success"))
            {
                rd=req.getRequestDispatcher("/success.jsp");    
            }
            else
            {
               //This is the point where i try to print error message on jsp.
                PrintWriter out = resp.getWriter(  );
                out.print("Sorry UserName or Password Error!");
                rd=req.getRequestDispatcher("/login.jsp");  
                rd.include(req, resp);  
            }
            rd.forward(req, resp);
        }   
    }

login.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Login Page</title>
    </head>
    <body>
        <form method="post" action="LoginController" >  
        UserName:   <input type="text" name="username"><BR><BR>
        PassWord:   <input type="password" name="password"><BR><BR>
                    <input type="submit" />   
        </form>
    </body>
    </html>

Please check the else block of loginController class where i am trying to print error message on my jsp file currently the "out.print" method is unable to reflect on my login.jsp file. Kindly help me to sort this issue Thanks in advance.

You can set the error message in request attribute and can fetch it in JSP

String errorMsg = "UserName or Password is invalid !";
req.setAttribute("errorMsg", errorMsg);
req.getRequestDispatcher("/login.jsp").forward(req, res);

Now on your JSP

<h2>${errorMsg}</h2>  // Print wherever you need it  
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mvc.demo.model.Authenticate;


public class LoginController extends HttpServlet {

    private static final long SerialVersionUID=1L;

    public LoginController()
    {
        super();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        String s1=req.getParameter("username");
        String s2=req.getParameter("password");
        RequestDispatcher rd=null;

        Authenticate au=new Authenticate();
        String result=au.authorise(s1, s2);

        if(result.equals("success"))
        {
            rd=req.getRequestDispatcher("/success.jsp");

//The error was with this line
            rd.forward(req, resp);
        }
        else
        {

            PrintWriter out = resp.getWriter(  );
            out.print("Sorry UserName or Password Error!");
            rd=req.getRequestDispatcher("/login.html");  
            rd.include(req, resp);  

        }

    }

}

I can't see out.close() in your code. You must close the PrintWriter object in the end.

If u want to print the error message in Jsp and first Set the Error msg in request

        req.setAttribute("msg","error msg configure here");

and now you can get the same in jsp by using EL Expression.

   ${msg} 

or You can get it by using scripting language.

 <%Object obj=request.getAttribute("msg");%>

Typecast the Obj into String and print by using this

             <%=str%>

Note:- Recommended to use El expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM