⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newuser.java

📁 《J2EE企业级应用开发》一书的配套源代码
💻 JAVA
字号:
package com.j2eeapp.cdstore.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;

import com.j2eeapp.cdstore.customer.*;
import com.j2eeapp.cdstore.vo.*;
import com.j2eeapp.cdstore.signon.*;
import com.j2eeapp.cdstore.util.JNDINames;
import com.j2eeapp.cdstore.bean.ShoppingSession;

public class NewUser extends HttpServlet
{
    
      InitialContext sl=null;
      private static final String CONTENT_TYPE = "text/html";
    /**
     * @J2EE_METHOD  --  doGet
     * Called by the server (via the service method) to allow a servlet to handle a GET request.
     * The servlet container must write the headers before committing the response, because
     * in HTTP the headers must be sent before the response body. The GET method should
     * be safe and idempotent. If the request is incorrectly formatted, doGet returns an
     * HTTP 'Bad Request' message.
     * @throws javax.servlet.ServletException
     * @throws java.io.IOException
     */
    public void doGet    (HttpServletRequest request, HttpServletResponse response) 
                throws ServletException, IOException 
    {
        String userid = request.getParameter("userid");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String street = request.getParameter("street");
        String city = request.getParameter("city");
        String state = request.getParameter("state");
        String zipCode = request.getParameter("zipCode");
        String cardNumber = request.getParameter("cardNumber");
        String cardType = request.getParameter("cardType");
        String banneropt = request.getParameter("banneropt");
        String fanvcategory = request.getParameter("fanvcategory");
        String expiryDate = request.getParameter("expiryDate");
        //System.out.println(Boolean.getBoolean(banneropt));
        //Account account=new Account(userid,email,AccountLocalHome.Active,street,city,state,zipCode,phone);
        Address address=new Address(street,city,state,zipCode);
        CreditCard creditCard=new CreditCard(cardNumber,cardType,new java.util.Date());
        ContactInfo contactInfo=new ContactInfo(name,phone,email,address);
        
       try
       {
       		 SignonLocalHome signonHome=(SignonLocalHome)sl.lookup(JNDINames.SIGNON);
       		 SignonLocal signon=signonHome.create();
        	 signon.createUser(userid,password);
        }
        catch(Exception e)
        {
        	  RequestDispatcher requestDispatcher = request.getRequestDispatcher(
	          "newuser?errorMessage=Username aleady exists.or error occur");
	        requestDispatcher.forward(request, response);
	        return;
	    }
	    try
	    {
	    	 	    	       		 
	    	 CreditCardLocalHome creditHome=(CreditCardLocalHome)sl.lookup(JNDINames.CREDITCARD);
	    	 CreditCardLocal credit=creditHome.create(creditCard);
	    	 if(credit!=null)System.out.println("credit is not null");
	    	 
	    	 ContactInfoLocalHome contactHome=(ContactInfoLocalHome)sl.lookup(JNDINames.CONTACT);
	    	 ContactInfoLocal contact=contactHome.create(contactInfo);
	    	 if(contact!=null)System.out.println("contact is not null");
	    	 
	    	 ProfileLocalHome profileHome=(ProfileLocalHome)sl.lookup(JNDINames.PROFILE);
	    	 ProfileLocal profile=profileHome.create(userid,fanvcategory,Boolean.getBoolean(banneropt));
	    	// ProfileLocal profile=profileHome.findByPrimaryKey(userid);
	    	 if(profile!=null)System.out.println("profile is not null");
	    	 
	    	/** AccountLocalHome accountHome=(AccountLocalHome)sl.lookup(JNDINames.ACCOUNT);
	    	 //AccountLocal account=accountHome.create(userid,AccountLocalHome.Active);
	    	  AccountLocal account=accountHome.findByPrimaryKey(userid);
	    	 if(account!=null)System.out.println("account is not null");**/
	    	 
	    	 CustomerLocalHome customerHome=(CustomerLocalHome)sl.lookup(JNDINames.CUSTOMER);
       	     CustomerLocal customer=customerHome.create(userid,contact,credit,profile);
       		 // CustomerLocal customer=customerHome.findByPrimaryKey(userid);
       		 if(customer!=null)System.out.println("customer is not null");
	    	 	    	
	    	       		 
       		// account.setContactInfo(contact);
       		// account.setCreditCard(credit);
       		 
       		// customer.setAccount(account);
         	// customer.setProfile(profile);
       		 ShoppingSession shoppingSession = (ShoppingSession) request.getSession().getAttribute("shoppingSession");
       		 try {
			      shoppingSession.login(userid, password);
			    }
			    catch (Exception ex) {
			      ex.printStackTrace();
			    }
       		  RequestDispatcher requestDispatcher = request.getRequestDispatcher(
	          "welcome");
	        requestDispatcher.forward(request, response);
	        
	        return;
       		 
       	}
       	catch(Exception e)
       	{
       		System.out.println(e.getMessage());
       		 RequestDispatcher requestDispatcher = request.getRequestDispatcher(
	          "newuser?errorMessage=error occur where create user!,please try again.");
	        requestDispatcher.forward(request, response);
	        return;
	     }

    }
    
    /**
     * @J2EE_METHOD  --  doPost
     * Called by the server (via the service method) to allow a servlet to handle a POST
     * request. The HTTP POST method allows the client to send data of unlimited length
     * to the Web server a single time and is useful when posting information such as credit
     * card numbers. If the HTTP POST request is incorrectly formatted, doPost returns
     * an HTTP 'Bad Request' message.
     * @throws javax.servlet.ServletException
     * @throws java.io.IOException
     */
    public void doPost    (HttpServletRequest request, HttpServletResponse response) 
                throws ServletException, IOException 
    {
		doGet(request,response);
    }
    
    /**
     * @J2EE_METHOD  --  NewUser
     */
    public NewUser    ()  throws NamingException
    { 
		sl=new InitialContext();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -