📄 housetypeservlet.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 HouseTypeServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
/*
protected HotelRemoteHome hotelRemoteHome;
protected HotelRemote hotelRemote;
protected HouseTypeRemoteHome houseTypeRemoteHome;
protected HouseTypeRemote houseTypeRemote;
protected Integer hotelPK = null;
//Initialize global variables
public void init() throws ServletException {
hotelRemoteHome = lookupHotelRemoteHome();
houseTypeRemoteHome = lookupHouseTypeRemoteHome();
HotelRemoteHome hotelRemoteHome = lookupHotelRemoteHome();
HouseTypeRemoteHome 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("addHouseType")){//----------------------------------------------------------------addHouseType
System.out.println("this is in HouseTypeServlet");
String houseName = request.getParameter("houseName");
String normalPrice = request.getParameter("normalPrice");
String vipPrice = request.getParameter("vipPrice");
String breakfast = request.getParameter("breakfast");
System.out.println ("+++++++++++++++++out put data begin++++++++++++++++++++++");
System.out.println (houseName);
System.out.println (normalPrice);
System.out.println (vipPrice);
System.out.println (breakfast);
System.out.println ("+++++++++++++++++out put data end ++++++++++++++++++++++");
//String str = request.getParameter("hotelPK");
//Integer hotelPK = Integer.decode(str);
Integer hotelPK = (Integer)session.getAttribute("session_hotelPK");
//把上面数据插入house_type表中
try {
HouseTypeRemoteHome houseTypeRemoteHome = lookupHouseTypeRemoteHome();
houseTypeRemoteHome.create(hotelPK,houseName,Integer.decode(normalPrice),Integer.decode(vipPrice),breakfast);
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println ("I can't insert into HouseType table!!");
}
try {
HouseTypeRemoteHome houseTypeRemoteHome = lookupHouseTypeRemoteHome();
Collection collection = houseTypeRemoteHome.findByHotelId(hotelPK);
Iterator it = collection.iterator();
request.setAttribute("session_it",it);
}
catch (Exception ex) {
ex.printStackTrace();
}
forward(request,response,"/jsp/hotel/AddHouseType.jsp");//跳转页面到AddHouseType.jsp
}
if(action.equals("addHouseType2")){//----------------------------------------------------------------addHouseType
System.out.println("this is in addHouseType2");
String houseName = request.getParameter("houseName");
String normalPrice = request.getParameter("normalPrice");
String vipPrice = request.getParameter("vipPrice");
String breakfast = request.getParameter("breakfast");
System.out.println ("+++++++++++++++++out put data begin++++++++++++++++++++++");
System.out.println (houseName);
System.out.println (normalPrice);
System.out.println (vipPrice);
System.out.println (breakfast);
System.out.println ("+++++++++++++++++out put data end ++++++++++++++++++++++");
//String str = request.getParameter("hotelPK");
//Integer hotelPK = Integer.decode(str);
Integer hotelPK = (Integer)session.getAttribute("session_hotelPK");
//把上面数据插入house_type表中
try {
HouseTypeRemoteHome houseTypeRemoteHome = lookupHouseTypeRemoteHome();
houseTypeRemoteHome.create(hotelPK,houseName,Integer.decode(normalPrice),Integer.decode(vipPrice),breakfast);
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println ("I can't insert into HouseType table!!");
}
try {
HouseTypeRemoteHome houseTypeRemoteHome = lookupHouseTypeRemoteHome();
Collection collection = houseTypeRemoteHome.findByHotelId(hotelPK);
Iterator it = collection.iterator();
request.setAttribute("session_it",it);
}
catch (Exception ex) {
ex.printStackTrace();
}
forward(request,response,"/jsp/hotel/UpdateHouseType.jsp");//UpdateHouseType.jsp
}
if(action.equals("delHouseType")){
String house_type_id = request.getParameter("house_type_id");
String hotelid = request.getParameter("hotelid");
System.out.println ("this is in delHouseType");
try {
HouseTypeRemoteHome houseTypeRemoteHome = lookupHouseTypeRemoteHome();
HouseTypeRemote houseTypeRemote=houseTypeRemoteHome.findByPrimaryKey(new Integer(house_type_id));
houseTypeRemote.remove();
HouseTypeRemoteHome houseTypeRemoteHome1 = lookupHouseTypeRemoteHome();
Collection collection = houseTypeRemoteHome1.findByHotelId(new Integer(hotelid));
Iterator it = collection.iterator();
request.setAttribute("session_it",it);
forward(request,response,"/jsp/hotel/UpdateHouseType.jsp");
}catch (Exception ex) {
ex.printStackTrace();
}
}
if(action.equals("showOneHouseType")){
String house_type_id = request.getParameter("house_type_id");
String hotelid = request.getParameter("hotelid");
System.out.println ("this is in updateHouseType");
System.out.println (house_type_id);
System.out.println (hotelid);
try {
HouseTypeRemoteHome houseTypeRemoteHome = lookupHouseTypeRemoteHome();
HouseTypeRemote houseTypeRemote=houseTypeRemoteHome.findByPrimaryKey(new Integer(house_type_id));
String houseName = houseTypeRemote.getHouseName();
String normalPrice = houseTypeRemote.getNormalPrice().toString();
String vipPrice = houseTypeRemote.getVipPrice().toString();
String breakfast = houseTypeRemote.getBreakfast();
request.setAttribute("houseName",houseName);
request.setAttribute("normalPrice",normalPrice);
request.setAttribute("vipPrice",vipPrice);
request.setAttribute("breakfast",breakfast);
request.setAttribute("house_type_id",house_type_id);
request.setAttribute("hotelid",hotelid);
forward(request,response,"/jsp/hotel/UpdateOneHouse.jsp");
}catch (Exception ex) {
ex.printStackTrace();
}
}
if(action.equals("updateHouseType")){
System.out.println ("this is in updateHouseType");
String hotelid = request.getParameter("hotelid");
Integer hotelPK = Integer.decode(hotelid);
String house_type_id = request.getParameter("house_type_id");
String houseName = request.getParameter("houseName");
String normalPrice = request.getParameter("normalPrice");
String vipPrice = request.getParameter("vipPrice");
String breakfast = request.getParameter("breakfast");
try {
HouseTypeRemoteHome houseTypeRemoteHome = lookupHouseTypeRemoteHome();
HouseTypeRemote houseTypeRemote=houseTypeRemoteHome.findByPrimaryKey(new Integer(house_type_id));
houseTypeRemote.setHouseName(houseName);
houseTypeRemote.setNormalPrice(new Integer(normalPrice));
houseTypeRemote.setVipPrice(new Integer(vipPrice));
houseTypeRemote.setBreakfast(breakfast);
session.setAttribute("session_hotelPK",hotelPK);
HouseTypeRemoteHome houseTypeRemoteHome2 = lookupHouseTypeRemoteHome();
Collection collection = houseTypeRemoteHome2.findByHotelId(hotelPK);
Iterator it = collection.iterator();
request.setAttribute("session_it",it);
forward(request,response,"/jsp/hotel/UpdateHouseType.jsp");
}catch (Exception ex) {
ex.printStackTrace();
}
}
/*
if(action.equals("addHouseType")){
System.out.println ("*****************in action addHouseType ***** begin**********************");
//接收house_type传来的数据
String houseName = request.getParameter("houseName");
String normalPrice = request.getParameter("normalPrice");
String vipPrice = request.getParameter("vipPrice");
String breakfast = request.getParameter("breakfast");
//hotelPK = (Integer)request.getAttribute("session_hotelPK");
System.out.println ("=======================begin========================");
System.out.println (houseName);
System.out.println (normalPrice);
System.out.println (vipPrice);
System.out.println (breakfast);
System.out.println ("=======================end========================");
//将收到的房间类型信息插入数据库
//please insert something ..............................................................................................................
//跳转换回AddHouseType.jsp进行下一次添加
//System.out.println ("go to HouseType!!!!");
System.out.println ("from action = addHouseType to addHouseType page");
System.out.println ("hotelPK is:"+hotelPK);
this.goHouseType(request,response);
//forward(request,response,"/jsp/hotel/AddHouseType.jsp");//跳转页面到AddHouseType.jsp
System.out.println ("*****************in action addHouseType ***** out**********************");
response.sendRedirect("./jsp/hotel/AddHouseType.jsp");
}
*/
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
private HotelRemoteHome lookupHotelRemoteHome() {
// Lookup the beans home using JNDI
HotelRemoteHome myHome = null;
try {
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
Context ctx = new InitialContext(ht);
Object home = ctx.lookup("HotelJndiName");
myHome = (HotelRemoteHome) PortableRemoteObject.narrow(home, HotelRemoteHome.class);
} catch (Exception e)
{
System.out.println("The client was unable to lookup the HotelRemoteHome.");
}
return myHome;
}
private HouseTypeRemoteHome lookupHouseTypeRemoteHome() {
// Lookup the beans home using JNDI
HouseTypeRemoteHome myHome = null;
try {
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
Context ctx = new InitialContext(ht);
Object home = ctx.lookup("HouseTypeJndiName");
myHome = (HouseTypeRemoteHome) PortableRemoteObject.narrow(home, HouseTypeRemoteHome.class);
} catch (Exception e)
{
System.out.println("The client was unable to lookup the HouseTypeRemoteHome.");
}
return myHome;
}
protected void forward(HttpServletRequest request,HttpServletResponse response,String url) throws ServletException,IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -