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.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.printfor HTML in a servlet. There the JSP is for. Set it as request attribute and just let JSP display it with help of EL.Rename
login.htmltologin.jspand add the following somewhere next to the submit button.See also: