AccessController.java
资源名称:MyStore.rar [点击查看]
上传用户:hgs128
上传日期:2007-02-03
资源大小:166k
文件大小:8k
源码类别:
百货/超市行业
开发平台:
WINDOWS
- /*
- * Created on 1999-5-19
- */
- package sevlet;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.rmi.RemoteException;
- import java.util.ArrayList;
- import java.util.Hashtable;
- import java.util.Iterator;
- import javax.ejb.CreateException;
- import javax.ejb.RemoveException;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import javax.rmi.PortableRemoteObject;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import cmp.ItemData;
- import stateless.StoreAccess;
- import stateless.StoreAccessHome;
- /**
- * @author 28-9
- *
- * TODO To change the template for this generated type comment go to
- * Window - Preferences - Java - Code Generation - Code and Comments
- */
- public class AccessController extends HttpServlet {
- private StoreAccessHome storeAccessHome=null;
- private StoreAccess myStore=null;
- private static String LOGIN_ERROR_SCREEN="/loginEror";
- private static String LOGIN_SCREEN="/login";
- private static String ITEMS_SCREEN="/showItems.jsp";
- public void init(ServletConfig config) throws ServletException {
- super.init(config);
- //TODO Method stub generated by Lomboz
- initStoreAccess();
- }
- /**
- *
- */
- private void initStoreAccess() {
- System.out.println("Entering AccessController.initStoreAccess()");
- try {
- storeAccessHome=getHome();
- } catch (NamingException e) {
- e.printStackTrace();
- }
- System.out.println("Leaving AccessController.initStoreAccess()");
- }
- /**
- * @return
- */
- private StoreAccessHome getHome() throws NamingException {
- Object result=getContext().lookup(StoreAccessHome.JNDI_NAME);
- return ((StoreAccessHome)PortableRemoteObject.narrow(result, StoreAccessHome.class));
- }
- private InitialContext getContext() throws NamingException{
- Hashtable props = new Hashtable();
- props.put(
- InitialContext.INITIAL_CONTEXT_FACTORY,
- "org.jnp.interfaces.NamingContextFactory");
- props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
- InitialContext initialContext = new InitialContext(props);
- return initialContext;
- }
- public void destroy() {
- super.destroy();
- //TODO Method stub generated by Lomboz
- }
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- processRequest(request,response);
- }
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- //TODO Method stub generated by Lomboz
- processRequest(request,response);
- }
- /**
- * @param request
- * @param response
- */
- private void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- System.out.println("Entering AccessController.processRequest()");
- String buildUrl=null;
- HttpSession session=request.getSession(true);
- String userAction=request.getParameter("useraction");
- if(userAction==null) {
- buildUrl=LOGIN_SCREEN;
- }else {
- if(userAction.equals("dovalidation")) {
- System.out.println("In validateLogin");
- String username=request.getParameter("username");
- String passwd=request.getParameter("passwd");
- if((username==null) || !(loginUser(username,passwd,session))) {
- System.out.println("Error:invalid attempt for username");
- buildUrl=LOGIN_ERROR_SCREEN;
- }else {
- buildUrl=ITEMS_SCREEN;
- }
- }
- }
- if(buildUrl==ITEMS_SCREEN) {
- String userID=(String)session.getAttribute("userID");
- try {
- myStore=storeAccessHome.create();
- ArrayList itemsList=myStore.getAllItems();
- //displayAllitems(response,itemsList);
- session.setAttribute("itemsList", itemsList);
- System.out.println("BuilderUrl is: "+buildUrl);
- RequestDispatcher rd=getServletContext().getRequestDispatcher(ITEMS_SCREEN);
- rd.forward(request, response);
- myStore.remove();
- } catch (RemoteException e) {
- e.printStackTrace();
- } catch (CreateException e) {
- e.printStackTrace();
- } catch (RemoveException e) {
- e.printStackTrace();
- }
- }else {
- if(buildUrl==LOGIN_SCREEN) {
- displayLoginScreen(response);
- }else if(buildUrl==LOGIN_ERROR_SCREEN) {
- displayLoginErrorScreen(response);
- }
- }
- System.out.println("Leaving AccessController.processRequest()");
- }
- /**
- * @param response
- */
- private void displayLoginErrorScreen(HttpServletResponse response) throws IOException {
- response.setContentType("text/html");
- PrintWriter out=response.getWriter();
- out.println("<html><title>MyStore Login Error</title>");
- out.println("<h3>Please try again,your login has failed!");
- out.println("<form method="get">");
- displayLoginDataFiels(out);
- out.println("</form><html>");
- if(out!=null) out.close();
- }
- /**
- * @param response
- */
- private void displayLoginScreen(HttpServletResponse response) throws IOException {
- System.out.println("Entering AccessController.displayLoginScreen()");
- response.setContentType("text/html");
- PrintWriter out=response.getWriter();
- out.println("<html><title>MyStore Login</title>");
- out.println("<body><h2>Welcome to MyStore</h2></body>");
- out.println("<form method="get">");
- displayLoginDataFiels(out);
- out.println("</form></html>");
- if(out!=null)
- out.close();
- System.out.println("Leaving AccessController.displayLoginScreen()");
- }
- /**
- * @param out
- */
- private void displayLoginDataFiels(PrintWriter out) {
- out.println("<h3>Please enter your username and password:");
- out.println("<table><tr><td>Username:<td><input name="username" type="text">");
- out.println("<tr><td>Password:<td><input name="passwd" type="password">");
- out.println("</table>");
- out.println("<input type="submit" value="login" name="loginButton">");
- out.println("<input type="reset" name="resetButton" value="reset">");
- out.println("<input type="hidden" name="useraction" value="dovalidation">");
- }
- /**
- * @param response
- * @param itemsList
- */
- private void displayAllitems(HttpServletResponse response, ArrayList itemsList) throws IOException {
- response.setContentType("text/html");
- PrintWriter out=response.getWriter();
- out.println("<html><title>List of MyStore items</title>");
- out.println("<form method="GET">");
- out.println("<body><h2>MyStore Items Cataloge</h2></body>");
- if(itemsList.isEmpty()) {
- out.println("<p><h3><b>Items are not avaliable</b></h3>");
- }else {
- out.println("<p><table border="1">");
- out.println("<tr><th><b>ItemID</b>");
- out.println("<th><b>Description</b>");
- out.println("<th><b>Quantity</b>");
- out.println("<th><b>Price</b>");
- Iterator items=itemsList.iterator();
- ItemData id=null;
- while(items.hasNext()) {
- id=(ItemData)items.next();
- out.println("<tr><td>");
- out.println(id.getItemID());
- out.println("</td><td>");
- out.println(id.getDescription());
- out.println("</td><td>");
- out.println(id.getQuantity());
- out.println("</td><td>");
- out.println(id.getPrice());
- out.println("</td></tr>");
- }
- out.println("<table>");
- }
- out.println("</html>");
- if(out!=null)
- out.close();
- }
- /**
- * @param username
- * @param passwd
- * @param session
- * @return
- */
- private boolean loginUser(String username, String passwd, HttpSession session) {
- String userid=null;
- StoreAccess sa;
- try {
- sa = storeAccessHome.create();
- userid=sa.loginUser(username, passwd);
- session.setAttribute("userID", userid);
- } catch (RemoteException e) {
- e.printStackTrace();
- } catch (CreateException e) {
- e.printStackTrace();
- }
- return (userid!=null);
- }
- }