Question 1 Marks10 You are required to write the code for sevlet which will take
ID: 3612274 • Letter: Q
Question
Question 1 Marks10
You are required to write the code for sevlet which will takeusername and password from the html file and matches with databasewhich contains two columns; one for username and other forpassword.
Providing correct information leads to welcome page otherwise toa login page again.
Question2 Marks5
Consider the following way for defining initializationparameters of servlet in web.xml.
<init-param>
<param-name> userName </param-name>
<param-value> admin </param-value>
</init-param>
<init-param>
<param-name> password </param-name>
<param-value> vu </param-value>
</init-param>
You are required to write the two ways for readinginitialization parameters from web.xml and display its value onhtml file.
Explanation / Answer
Dear...Question 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;
}
}
Question 2: <!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN""http://java.sun.com/j2ee/dtds/web app_2.2.dtd"> <web-app>
<display-name>A Test app for parameterpassing</display-name> <context-param> <param-name>Username</param-name> <param-value>admin</param-value> <description> Thisparameter is here to test the web.xml functionality </description> </context-param> <servlet> <servlet-name>Hello</servlet-name> <servlet-class>HelloServlet.class</servlet-class> <init-param> <param-name>password </param-name> <param-value>uv</param-value> </init-param> </servlet> </web-app>> <!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN""http://java.sun.com/j2ee/dtds/web app_2.2.dtd"> <web-app>
<display-name>A Test app for parameterpassing</display-name> <context-param> <param-name>Username</param-name> <param-value>admin</param-value> <description> Thisparameter is here to test the web.xml functionality </description> </context-param> <servlet> <servlet-name>Hello</servlet-name> <servlet-class>HelloServlet.class</servlet-class> <init-param> <param-name>password </param-name> <param-value>uv</param-value> </init-param> </servlet> </web-app>> I hope it is useful yoyou
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.