Question You are required to write the code for sevlet which willtake username a
ID: 3612340 • Letter: Q
Question
Question
You are required to write the code for sevlet which willtake username and password from the html file and matches withdatabase which contains two columns; one for username and other forpassword.
Providing correct information leads to welcome pageotherwise to a login page again.
You are required to write the code for servletonly.
QuestionDetails:Question
You are required to write the code for sevlet which willtake username and password from the html file and matches withdatabase which contains two columns; one for username and other forpassword.
Providing correct information leads to welcome pageotherwise to a login page again.
You are required to write the code for servletonly.
Explanation / Answer
Dear.... 1) import java.io.IOException; import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginHandler extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String account = req.getParameter("account");
String password = req.getParameter("password");
String pin = req.getParameter("pin");
if (!allowUser(account, password, pin)) {
out.println("<HTML><HEAD><TITLE>Access Denied</TITLE></HEAD>");
out.println("<BODY>Your login and password are invalid.<BR>");
out.println("You may want to <A href="/login.html">try again</A>");
out.println("</BODY></HTML>");
} else {
HttpSession session = req.getSession();
session.setAttribute("logon.isDone", account);
try {
String target = (String) session.getAttribute("login.target");
if (target != null) { res.sendRedirect(target); return; } } catch (Exception ignored) {
---- ---- }
res.sendRedirect("/");
}
}
protected boolean allowUser(String account, String password, String pin) {
return true;
}
} 1) import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginHandler extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String account = req.getParameter("account");
String password = req.getParameter("password");
String pin = req.getParameter("pin");
if (!allowUser(account, password, pin)) {
out.println("<HTML><HEAD><TITLE>Access Denied</TITLE></HEAD>");
out.println("<BODY>Your login and password are invalid.<BR>");
out.println("You may want to <A href="/login.html">try again</A>");
out.println("</BODY></HTML>");
} else {
HttpSession session = req.getSession();
session.setAttribute("logon.isDone", account);
try {
String target = (String) session.getAttribute("login.target");
if (target != null) { res.sendRedirect(target); return; } } catch (Exception ignored) {
---- ---- }
res.sendRedirect("/");
}
}
protected boolean allowUser(String account, String password, String pin) {
return true;
}
} I hope it is useful to you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.