📄 hotelservlet.java
字号:
package com.abc.hotel;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.rmi.*;
import javax.ejb.*;
public class HotelServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
/*
protected HotelRemoteHome hotelRemoteHome;
protected HotelRemote hotelRemote;
protected HouseTypeRemoteHome houseTypeRemoteHome;
protected HouseTypeRemote houseTypeRemote;
HotelRemoteHome hotelRemoteHome = lookupHotelRemoteHome();
HouseTypeRemoteHome houseTypeRemoteHome = lookupHouseTypeRemoteHome();
HotelRemote
*/
//Initialize global variables
/*
public void init() throws ServletException {
hotelRemoteHome = lookupHotelRemoteHome();
houseTypeRemoteHome = lookupHouseTypeRemoteHome();
super.init();
}
*/
//Process the HTTP Get request
//HttpSession session = new HttpSession();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
HttpSession session = request.getSession();
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String action = (String)request.getParameter("action");
if(action.equals("findAll")){ //---------------------------------------------------------------------findall
System.out.println ("this is find All");
try {
HotelRemoteHome hotelRemoteHome = lookupHotelRemoteHome();//////////////////////////////////////////
Collection collection = hotelRemoteHome.findAll();
request.setAttribute("session_findAll",collection);
HouseTypeRemoteHome houseTypeRemoteHome = lookupHouseTypeRemoteHome();
request.setAttribute("session_houseTypeRemoteHome",houseTypeRemoteHome);
forward(request,response,"/jsp/hotel/showall.jsp");
}
catch (Exception ex) {
out.println("Sorry I can not list all hotel");
}
}
if(action.equals("findone")){//----------------------------------------------------------------------findone
System.out.println ("in find one");
String hotelid = request.getParameter("hotelid");
try {
HotelRemoteHome hotelRemoteHome = lookupHotelRemoteHome();/////////////////////////////////////////////
HotelRemote hotelRemote = hotelRemoteHome.findByPrimaryKey(new Integer(hotelid));//////////////////////
String name = hotelRemote.getName();
String hotelDesc = hotelRemote.getHotelDesc();
String pic = hotelRemote.getPic();
String hotelLevel = hotelRemote.getHotelLevel();
String city = hotelRemote.getCity();
String address = hotelRemote.getAddress();
String traffic = hotelRemote.getTraffic();
String fw = hotelRemote.getFw();
String hy = hotelRemote.getHy();
String cy = hotelRemote.getCy();
String xx = hotelRemote.getXx();
String cCard = hotelRemote.getCcard();
request.setAttribute("session_hotelid",hotelid);
request.setAttribute("session_name",name);
request.setAttribute("session_hotelDesc",hotelDesc);
request.setAttribute("session_pic",pic);
request.setAttribute("session_hotelLevel",hotelLevel);
request.setAttribute("session_city",city);
request.setAttribute("session_address",address);
request.setAttribute("session_traffic",traffic);
request.setAttribute("session_fw",fw);
request.setAttribute("session_hy",hy);
request.setAttribute("session_cy",cy);
request.setAttribute("session_xx",xx);
request.setAttribute("session_cCard",cCard);
HouseTypeRemoteHome houseTypeRemoteHome = lookupHouseTypeRemoteHome();//////////////////////////////////
Collection collection = houseTypeRemoteHome.findByHotelId(new Integer(hotelid));
Iterator iterator = collection.iterator();
request.setAttribute("session_hotel_type_iterator",iterator);
forward(request,response,"/jsp/hotel/HotelInfo.jsp");
}
catch (Exception ex) {
out.println("some error in find one");
}
}
if(action.equals("findByName")){//------------------------------------------------------------------findByName
System.out.println ("this is in findByName");
String name = request.getParameter("name");
try {
HotelRemoteHome hotelRemoteHome = lookupHotelRemoteHome();/////////////////////////////////////////////
HotelRemote hotelRemote = hotelRemoteHome.findByName(name);
request.setAttribute("session_findByName",hotelRemote);
forward(request,response,"/jsp/hotel/showByName.jsp");
}
catch (Exception ex) {
out.println("I can not find by name");
}
}
if(action.equals("addHotel")){//---------------------------------------------------------------------addHotel
System.out.println ("this is addHotel");
String name = request.getParameter("name");
String hotelDesc = request.getParameter("hotelDesc");
String pic = request.getParameter("pic");
String hotelLevel = request.getParameter("hotelLevel");
String city = request.getParameter("city");
String address = request.getParameter("address");
String traffic = request.getParameter("traffic");
String fw = request.getParameter("fw");
String hy = request.getParameter("hy");
String cy = request.getParameter("cy");
String xx = request.getParameter("xx");
String cCard = request.getParameter("cCard");
//把以上结果插入数据库 表hotel
try {
HotelRemoteHome hotelRemoteHome = lookupHotelRemoteHome();////////////////////////////////////////////
Integer hotelPK = (Integer)hotelRemoteHome.create(name,pic,hotelDesc,city,hotelLevel,cCard,cy,xx,hy,fw,traffic,address).getPrimaryKey();
//System.out.println (hotelPK.toString());
session.setAttribute("session_hotelPK",hotelPK);
HouseTypeRemoteHome houseTypeRemoteHome = lookupHouseTypeRemoteHome();
Collection collection = houseTypeRemoteHome.findByHotelId(hotelPK);
Iterator it = collection.iterator();
request.setAttribute("session_it",it);//将it插入session用于在jsp页面中显示此酒店已经添加的房间类型相信资料
System.out.println ("go to AddHouseType.jsp");
forward(request,response,"/jsp/hotel/AddHouseType.jsp");//跳转页面到AddHouseType.jsp
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println ("I can't insert into Hotel table!!");
}
System.out.println ("add hotel_table date in oracle ok!");
/*
try {
//String namek = "中国大饭店";
HotelRemoteHome hotelRemoteHome = lookupHotelRemoteHome();///////////////////////////////////////////////
HotelRemote hotelRemote = hotelRemoteHome.findByName(name);//测试结束后将namek改名为name
Integer hotelPK = (Integer)hotelRemote.getPrimaryKey(); //得到当前hotelid
session.setAttribute("session_hotelPK",hotelPK);//将当前hotelid插入session用于插入house_type
HouseTypeRemoteHome houseTypeRemoteHome = lookupHouseTypeRemoteHome();
Collection collection = houseTypeRemoteHome.findByHotelId(hotelPK);
Iterator it = collection.iterator();
request.setAttribute("session_it",it);//将it插入session用于在jsp页面中显示此酒店已经添加的房间类型相信资料
System.out.println ("go to AddHouseType.jsp");
forward(request,response,"/jsp/hotel/AddHouseType.jsp");//跳转页面到AddHouseType.jsp
}
catch (Exception ex) {
ex.printStackTrace();
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -