📄 loginservlet.java
字号:
package com.wiley.compBooks.EJwithUML.TimecardUI;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.naming.*;
import javax.ejb.*;
import java.rmi.*;
import javax.rmi.PortableRemoteObject;
import java.io.*;
import com.wiley.compBooks.EJwithUML.Base.HtmlPrimitives.Core.*;
import com.wiley.compBooks.EJwithUML.Base.HtmlPrimitives.Layout.*;
import com.wiley.compBooks.EJwithUML.Base.HtmlPrimitives.ContentElements.*;
import com.wiley.compBooks.EJwithUML.Base.EjbUtil.*;
import com.wiley.compBooks.EJwithUML.Base.ApplicationExceptions.*;
import com.wiley.compBooks.EJwithUML.TimeCardWorkflow.*;
import com.wiley.compBooks.EJwithUML.TimecardProducers.*;
/**
*
* The LoginServlet class uses the TimecardWorkflow and
* HtmlPrimitives packages to create the formatted HTML
* for the Login form and to validate the user.
*
*/
public class LoginServlet extends BasicTimecardServlet
{
private LoginWorkflowHome lwfHome = null;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
/** Overrides method from HttpServlet.
doPost is called by the servlet engine. */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
System.out.println("LoginServlet: doPost");
PagePrimitive pagePrimitive = null;
try
{
// extract user name and password
String username = request.getParameter(TimecardKey.USERNAME.getKeyText());
String password = request.getParameter(TimecardKey.PASSWORD.getKeyText());
if (username == null)
{
pagePrimitive = buildLoginPage();
}
else
{
if (this.isLoginValid(username, password))
{
HttpSession session = request.getSession(true);
session.setAttribute(TimecardKey.USERNAME.getKeyText(), username);
pagePrimitive = buildWelcomePage(username);
}
else
{
pagePrimitive = buildLoginInvalidPage();
}
}
}
catch (NamingException e)
{
pagePrimitive = this.getErrorPage("Error During Login\n", e);
}
catch (CreateException e)
{
pagePrimitive = this.getErrorPage("Error During Login\n", e);
}
StringBuffer buffer = new StringBuffer();
pagePrimitive.buildContent(buffer);
response.getWriter().println(buffer);
response.getWriter().flush();
response.getWriter().close();
}
/** Build page for display on succesful login.*/
PagePrimitive buildWelcomePage(String username)
{
PagePrimitive page = new TimecardPageProducer("Welcome");
ParagraphPrimitive welcome = new ParagraphPrimitive(TimecardStyle.PAGE_TITLE, "Welcome, " +username);
page.addToBody(welcome);
LinkPrimitive link = new LinkPrimitive("/Timecard/RecordTimeServlet");
link.addText("Enter Hours");
page.addToBody(link);
return page;
}
/** Build page for user input */
PagePrimitive buildLoginPage()
{
SimpleInputFormProducer form = new SimpleInputFormProducer("Login", "Please enter your username and password.","Login", "./LoginServlet");
form.addField("User Name", TimecardKey.USERNAME.getKeyText(), "");
form.addMaskedField("Password", TimecardKey.PASSWORD.getKeyText(), "");
PagePrimitive page = new TimecardPageProducer("Login");
page.addToBody(form);
return page;
}
/** Build page with error message when login fails. */
PagePrimitive buildLoginInvalidPage()
{
PagePrimitive page = buildLoginPage();
SpanPrimitive errorMessage = new SpanPrimitive(Style.WARNING_TEXT, "Invalid username or password. Remember, case matters.");
page.addToBody(errorMessage);
return page;
}
/** Answer true if the login data matches the database.*/
private boolean isLoginValid(String username, String password) throws NamingException, RemoteException, CreateException
{
boolean valid = false;
try
{
createLoginWorkflowHome();
LoginWorkflow lwf = lwfHome.create();
valid = lwf.isUserValid(username, password);
}
catch (DataNotFoundException e)
{
valid = false;
}
return valid;
}
private void createLoginWorkflowHome() throws NamingException
{
if (lwfHome == null)
{
Context initial = new InitialContext();
Object objref = initial.lookup(EjbReferenceNames.LOGIN_WORKFLOW_HOME);
lwfHome = (LoginWorkflowHome)PortableRemoteObject.narrow(objref,LoginWorkflowHome.class);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -