开发者

Servlet -print invalid password in the same page

I have tried a program where if username matches password, welcome message should be displayed in someother page. If it doesn,t match error should be in the same page. I have tried this

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class WelcomeUser extends HttpServlet {
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String name=request开发者_如何学C.getParameter("name");
        String pwd=request.getParameter("pwd");
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();

        if (!(name.equals(pwd))) {
            out.println("Invalid");
            request.getRequestDispatcher("login.html").forward(request,response);
        } else {
            out.println("<html>");
            out.println("<body>");
            out.println("Welcome"+name);
            out.println("</html>");
            out.println("</body>");
        }
    }
}

What change should I make to print invalid user in the same page? It compiles fine but I am not getting invalid in the login page


Don't ever use out.print for HTML in a servlet. There the JSP is for. Set it as request attribute and just let JSP display it with help of EL.

request.setAttribute("message", "Invalid"); // Will be available as ${message}
request.getRequestDispatcher("login.jsp").forward(request,response);

Rename login.html to login.jsp and add the following somewhere next to the submit button.

${message}

See also:

  • Servlets tag info page - contains hello world example and several useful links.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜