📄 itemlistservlet.java
字号:
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.rmi.*;
import javax.naming.*;
import java.util.*;
public class ItemListServlet extends HttpServlet
{
ItemHome itemHome;
public void init() throws ServletException
{
try
{
System.out.println("trying to get initial context");
Context ic = (InitialContext) getInitialContext();
System.out.println("Got InitContext");
Object objRef = ic.lookup("Item");
System.out.println("Got obj ref");
itemHome = (ItemHome) PortableRemoteObject.narrow(objRef,ItemHome.class);
System.out.println("Got Home");
}catch(Exception e)
{
System.out.println("Error in init");
e.printStackTrace();
}
}
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
try
{
HttpSession session = req.getSession(false);
String clientCode = (String)req.getParameter("client");
session.setAttribute("ClientId",clientCode);
Collection items = itemHome.findAll();
if(items.size() <= 0)
{
System.out.println("items size: " + items.size());
res.sendRedirect(res.encodeURL("/Status.jsp?code=3"));
}
Vector itemInfo[] = new Vector[items.size()];
System.out.println("length of item list: " + items.size());
Iterator i = items.iterator();
int cnt=0;
System.out.println("Beginning search");
while(i.hasNext())
{
Item item = (Item) i.next();
itemInfo[cnt] = new Vector(3);
itemInfo[cnt].add(item.getId());
itemInfo[cnt].add(item.getName());
itemInfo[cnt].add(item.getDescription());
cnt++;
}
session.setAttribute("ItemList",itemInfo);
System.out.println("redirecting to itemlist jsp");
res.sendRedirect(res.encodeURL("/ItemList.jsp"));
}catch(Exception e){
System.out.println("Error while obtaining item list:" + e.getMessage());
res.sendRedirect(res.encodeURL("/Status.jsp?code=1"));
}
}
private Context getInitialContext() throws NamingException
{
Properties h = null;
try {
// Get an InitialContext
h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, "t3://localhost:7001");
}catch(Exception ne) {
System.out.println("Unable to get an initial context");
}
return new InitialContext(h);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -