StudentClient.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:15k
源码类别:
Java编程
开发平台:
Java
- package bmpsample;
- //package j2eebootcamp.developingEJB.chapter9.web.servlet;
- /**
- *
- * StudentClient
- *
- *
- * Author: Pravin Tulachan, pvt@javacamp.com
- * Date: Sept 15 2001
- * Version: 1.0
- *
- */
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.io.*;
- import javax.jms.*;
- import javax.naming.*;
- import java.sql.*;
- import java.util.*;
- import java.rmi.*;
- import javax.rmi.PortableRemoteObject;
- import javax.ejb.*;
- //import j2eebootcamp.developingEJB.chapter9.student.StudentHome;
- //import j2eebootcamp.developingEJB.chapter9.student.Student;
- public class StudentClient
- extends HttpServlet {
- Context jndictx = null;
- private StudentBmpHome studentHome = null;
- private StudentBmp student = null;
- private String primaryKey = null;
- String password = null;
- String first = null;
- String last = null;
- String email = null;
- String phone = null;
- String company = null;
- public void init() {
- //look up jndi context
- try {
- jndictx = new InitialContext();
- studentHome = (StudentBmpHome) PortableRemoteObject.narrow(jndictx.lookup(
- "StudentBmp"), StudentBmpHome.class);
- }
- catch (NamingException ne) {
- System.out.println(" Error creating JNDI context: " + ne.toString());
- System.exit(1);
- }
- catch (Exception e) {
- System.out.println(" init() :" + e.getMessage());
- System.exit(1);
- }
- }
- public void doPost(HttpServletRequest req, HttpServletResponse resp) {
- String action = req.getParameter("requestAction");
- if (action.equals("CreateStudent")) {
- System.out.println(" Servlet -- Create Student ");
- createStudent(req, resp);
- }
- else if (action.equals("GetCollection")) {
- System.out.println("--- StudentClient -- look for collection ---");
- searchByCompany(req, resp);
- }
- else if (action.equals("ListSchedules")) {
- System.out.println("--- StudentClient -- list schedules---");
- try {
- listAllClasses(req, resp);
- }
- catch (RemoteException re) {
- System.out.println(" remote exception " + re.getMessage());
- }
- }
- else if (action.equals("AddSchedule")) {
- System.out.println("--- StudentClient -- add a schedue to the Roster ---");
- try {
- addAClass(req, resp);
- }
- catch (RemoteException re) {
- System.out.println(" remote exception " + re.getMessage());
- }
- }
- else if (action.equals("DeleteSchedule")) {
- System.out.println(
- "--- StudentClient -- remove a schedule from the Roster ---");
- try {
- deleteAClass(req, resp);
- }
- catch (RemoteException re) {
- System.out.println(" remote exception " + re.getMessage());
- }
- }
- else {
- try {
- student = studentHome.findByPrimaryKey(primaryKey);
- if (action.equals("ModifyPassword")) {
- String newPassword = req.getParameter("NewPassword");
- modifyPassword(req, resp, newPassword);
- }
- else if (action.equals("ModifyFirstName")) {
- String newFirstName = req.getParameter("NewFirstName");
- modifyFirstName(req, resp, newFirstName);
- }
- else if (action.equals("ModifyLastName")) {
- String newLastName = req.getParameter("NewLastName");
- modifyLastName(req, resp, newLastName);
- }
- else if (action.equals("ModifyPhone")) {
- String newPhone = req.getParameter("NewPhone");
- modifyPhone(req, resp, newPhone);
- }
- else if (action.equals("ModifyEmail")) {
- String newEmail = req.getParameter("NewEmail");
- modifyEmail(req, resp, newEmail);
- }
- else if (action.equals("ModifyCompanyName")) {
- String newCompanyName = req.getParameter("NewCompanyName");
- modifyCompanyName(req, resp, newCompanyName);
- }
- else if (action.equals("RemoveStudent")) {
- String studentID = req.getParameter("StudentID");
- removeStudent(req, resp, studentID);
- }
- else {
- System.out.println("StudentClient -- Unknown request!");
- }
- }
- catch (FinderException fe) {
- System.out.println(" StudentClient finder exception =" + fe.getMessage());
- }
- catch (RemoteException re) {
- System.out.println(" StudentClient remote exception =" + re.getMessage());
- }
- catch (Exception e) {
- System.out.println(" StudentClient exception =" + e.getMessage());
- }
- } //if-else
- }
- public void createStudent(HttpServletRequest req, HttpServletResponse resp) {
- // read the input parameters and then call appropriate method method
- System.out.println("************ StudentClient ************n");
- primaryKey = req.getParameter("PrimaryKey");
- password = req.getParameter("Password");
- first = req.getParameter("First");
- last = req.getParameter("Last");
- email = req.getParameter("Email");
- phone = req.getParameter("Phone");
- company = req.getParameter("Company");
- try {
- //access the EJB
- System.out.println("****** StudentClient() -- before create() ***");
- student = studentHome.create(primaryKey, password, first, last, email,
- phone, company);
- //set attribute to pass it off to jsp page
- req.setAttribute("PrimaryKey", primaryKey);
- req.setAttribute("OldPassword", password);
- req.setAttribute("OldFirstName", first);
- req.setAttribute("OldLastName", last);
- req.setAttribute("OldEmail", email);
- req.setAttribute("OldPhone", phone);
- req.setAttribute("OldCompanyName", company);
- //call the JSP
- RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
- "/ModifyStudent.jsp");
- dispatcher.forward(req, resp);
- }
- catch (CreateException ce) {
- System.out.println(" create exception =" + ce.getMessage());
- }
- catch (ServletException e) {
- System.out.println("servlet exception =" + e.getMessage());
- }
- catch (RemoteException e) {
- System.out.println(" remote exception =" + e.getMessage());
- }
- catch (IOException e) {
- System.out.println(" io exception =" + e.getMessage());
- }
- }
- public void searchByCompany(HttpServletRequest req, HttpServletResponse resp) {
- // read the input parameters and then call appropriate method method
- System.out.println("nn************ StudentClient ************n");
- company = req.getParameter("Company");
- try {
- //access the EJB
- System.out.println(
- "****** StudentClient() -- before searchByCompany() ***");
- ArrayList primaryKeyList = (ArrayList) studentHome.findByCompanyName(
- company);
- Iterator i = primaryKeyList.iterator();
- while (i.hasNext()) {
- StudentBmp stud = (StudentBmp) i.next();
- String studentID = (String) stud.getPrimaryKey();
- System.out.println("StudentClient list - collection id : " + studentID +
- "n");
- }
- req.setAttribute("pKeyList", primaryKeyList);
- req.setAttribute("companyName", company);
- //call the JSP
- RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
- "/DisplayCollection.jsp");
- dispatcher.forward(req, resp);
- }
- catch (FinderException fe) {
- System.out.println(" finder exception =" + fe.getMessage());
- }
- catch (ServletException e) {
- System.out.println("servlet exception =" + e.getMessage());
- }
- catch (RemoteException e) {
- System.out.println(" remote exception =" + e.getMessage());
- }
- catch (IOException e) {
- System.out.println(" io exception =" + e.getMessage());
- }
- }
- public void modifyPassword(HttpServletRequest req, HttpServletResponse resp,
- String newPassword) throws RemoteException {
- System.out.println(" *** StudentClient -- modidfyPassword() newPassowrd = " +
- newPassword);
- student.setPassword(newPassword);
- password = student.getPassword();
- callJSP(req, resp);
- }
- public void modifyCompanyName(HttpServletRequest req,
- HttpServletResponse resp, String newCompanyName) throws
- RemoteException {
- System.out.println(
- " *** StudentClient -- modidfyCompanyName() newCompanyName = " +
- newCompanyName);
- student.setCompanyName(newCompanyName);
- String changedCompanyName = student.getCompanyName();
- System.out.println(" modidfyCompanyName() Get New CompanyName =" +
- changedCompanyName);
- company = changedCompanyName;
- callJSP(req, resp);
- }
- public void modifyFirstName(HttpServletRequest req, HttpServletResponse resp,
- String newFirstName) throws RemoteException {
- System.out.println(
- " *** StudentClient -- modidfyFirstName() newFirstName = " +
- newFirstName);
- student.setFirstName(newFirstName);
- String changedFirstName = student.getFirstName();
- System.out.println(" modidfyFirstName() Get New Firstname =" +
- changedFirstName);
- first = changedFirstName;
- callJSP(req, resp);
- }
- public void modifyLastName(HttpServletRequest req, HttpServletResponse resp,
- String newLastName) throws RemoteException {
- System.out.println(" *** StudentClient -- modidfyLastName() newLastName = " +
- newLastName);
- student.setLastName(newLastName);
- String changedLastName = student.getLastName();
- System.out.println(" modidfyLastName() Get New LastName =" +
- changedLastName);
- last = changedLastName;
- callJSP(req, resp);
- }
- public void modifyPhone(HttpServletRequest req, HttpServletResponse resp,
- String newPhone) throws RemoteException {
- System.out.println(" *** StudentClient -- modidfyPhone() newPhone = " +
- newPhone);
- student.setPhone(newPhone);
- String changedPhone = student.getPhone();
- System.out.println(" modidfyPhone() Get New Phone =" + changedPhone);
- phone = changedPhone;
- callJSP(req, resp);
- }
- public void modifyEmail(HttpServletRequest req, HttpServletResponse resp,
- String newEmail) throws RemoteException {
- System.out.println(" *** StudentClient -- modidfyEmail() newEmail = " +
- newEmail);
- student.setEmail(newEmail);
- String changedEmail = student.getEmail();
- System.out.println(" modidfyEmail() Get New Email =" + changedEmail);
- email = changedEmail;
- callJSP(req, resp);
- }
- public void removeStudent(HttpServletRequest req, HttpServletResponse resp,
- String studentID) throws RemoteException {
- System.out.println("n *** StudentClient -- removeStudent() studentID = " +
- studentID);
- try {
- student.remove();
- req.setAttribute("removed", studentID);
- System.out.println(" removed a student ");
- //call the JSP
- RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
- "/ConfirmDelete.jsp");
- dispatcher.forward(req, resp);
- }
- catch (RemoveException e) {
- System.out.println(" remove exception " + e.getMessage());
- }
- catch (ServletException e) {
- System.out.println("servlet exception =" + e.getMessage());
- }
- catch (RemoteException e) {
- System.out.println(" remote exception =" + e.getMessage());
- }
- catch (IOException e) {
- System.out.println(" io exception =" + e.getMessage());
- }
- }
- private void callJSP(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("n*** in StudentClient -- callJSP() **********");
- //System.out.println(" -- firstName = "+first+", lastName="+last);
- //set attribute to pass it off to jsp page
- req.setAttribute("PrimaryKey", primaryKey);
- req.setAttribute("OldPassword", password);
- req.setAttribute("OldFirstName", first);
- req.setAttribute("OldLastName", last);
- req.setAttribute("OldEmail", email);
- req.setAttribute("OldPhone", phone);
- req.setAttribute("OldCompanyName", company);
- try {
- RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
- "/ModifyStudent.jsp");
- dispatcher.forward(req, resp);
- }
- catch (ServletException se) {
- System.out.println(" *** StudentClient dispatcher error =" +
- se.getMessage());
- }
- catch (Exception e) {
- System.out.println(" error =" + e.getMessage());
- }
- }
- // --------------------- roster related calls ----------------------
- public void addAClass(HttpServletRequest req, HttpServletResponse resp) throws
- RemoteException {
- System.out.println(" *** StudentClient -- addAClass() --------- ");
- String schedID = req.getParameter("ScheduleID");
- try {
- student.addASchedule(schedID);
- }
- catch (RosterDAOException ex) {
- }
- listAllClasses(req, resp);
- }
- public void deleteAClass(HttpServletRequest req, HttpServletResponse resp) throws
- RemoteException {
- System.out.println(" *** StudentClient -- deleteAClass() --------- ");
- String schedID = req.getParameter("ScheduleID");
- try {
- student.deleteASchedule(schedID);
- }
- catch (RosterDAOException ex) {
- }
- listAllClasses(req, resp);
- }
- public void listAllClasses(HttpServletRequest req, HttpServletResponse resp) throws
- RemoteException {
- Vector list = new Vector(20);
- try {
- list = student.getScheduleList();
- }
- catch (RosterDAOException ex) {
- }
- System.out.println(
- " *** StudentClient -- listAllClasses() ------- size => " + list.size());
- for (int i = 0; i < list.size(); i++) {
- System.out.println("-- StudentClient -- schedule -->" + list.get(i));
- }
- req.setAttribute("schedList", list);
- try {
- RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
- "/DisplayClassList.jsp");
- dispatcher.forward(req, resp);
- }
- catch (ServletException se) {
- System.out.println(" *** StudentClient dispatcher error =" +
- se.getMessage());
- }
- catch (Exception e) {
- System.out.println(" error =" + e.getMessage());
- }
- }
- } //;-) end of the Servlet client.