?? cateservlet.java
字號:
/*
* Created on 2008-6-8
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package cn.edu.zucc.research.servlet;
import java.io.IOException;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.edu.zucc.research.ejb.session.cate.*;
import cn.edu.zucc.research.model.ResearchCate;
public class CateServlet extends HttpServlet {
ResearchCateS dao=null;
private long seed = 1;
private SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
private ResearchCateSHome getHome()throws NamingException {
return (ResearchCateSHome) getContext().lookup(ResearchCateSHome.JNDI_NAME);
}
private InitialContext getContext() throws NamingException {
Hashtable props = new Hashtable();
props.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
// This establishes the security for authorization/authentication
// props.put(InitialContext.SECURITY_PRINCIPAL,"username");
// props.put(InitialContext.SECURITY_CREDENTIALS,"password");
InitialContext initialContext = new InitialContext(props);
//System.out.println("gjy4:"+initialContext);
return initialContext;
}
public void init() throws ServletException {
try{
dao=getHome().create();
}catch(RemoteException e){
e.printStackTrace();
}catch(CreateException e){
e.printStackTrace();
}catch(NamingException e){
e.printStackTrace();
}
//TODO Method stub generated by Lomboz
}
public void destroy() {
// TODO Auto-generated method stub
try{
dao.remove();
}catch(Exception e){}
super.destroy();
}
public java.lang.String getServletInfo() {
//TODO Method stub generated by Lomboz
return super.getServletInfo();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//TODO Method stub generated by Lomboz
this.doPost(request, response);
}
private String listResearchCate(HttpServletRequest request){
try{
request.setAttribute("objlist",dao.loadAll());
System.out.println("列出成功!");
return "/researchCate_list.jsp";
}catch(Exception ex){
ex.printStackTrace();
request.setAttribute("errormsg", ex.getMessage());
return "/error.jsp";
}
}
private String addResearchCate(HttpServletRequest request){
try{
ResearchCate resc=new ResearchCate();
resc.setMethod("addResearchCateResult");
request.setAttribute("researchCate",resc);
System.out.println("add");
return "/researchCate_edit.jsp";
}catch(Exception ex){
ex.printStackTrace();
request.setAttribute("errormsg", ex.getMessage());
return "/error.jsp";
}
}
private String addResearchCateResult(HttpServletRequest request){
try{
ResearchCate researchCate =new ResearchCate();
researchCate.setCategoryId(df.format(new Date()) + "-" + (seed++));
System.out.println("categoryid---"+researchCate.getCategoryId());
String name=request.getParameter("categoryName");
System.out.println("categoryName---"+name);
researchCate.setCategoryName(name);
this.dao.saveCate(researchCate);
System.out.println("save---success");
return this.listResearchCate(request);
}catch(Exception ex){
ex.printStackTrace();
request.setAttribute("errormsg", ex.getMessage());
return "/error.jsp";
}
}
private String modifyResearchCate(HttpServletRequest request){
try{
String id=request.getParameter("id");
System.out.println("mod--read--id---"+id);
ResearchCate resCate=this.dao.readCate(id);
System.out.println("read---"+resCate.getCategoryId());
resCate.setMethod("modifyResearchCateResult");
request.setAttribute("researchCate",resCate);
return "/researchCate_edit.jsp";
}catch(Exception ex){
ex.printStackTrace();
request.setAttribute("errormsg", ex.getMessage());
return "/error.jsp";
}
}
private String modifyResearchCateResult(HttpServletRequest request){
try{
ResearchCate researchCate =new ResearchCate();
researchCate.setCategoryId(request.getParameter("categoryId"));
researchCate.setCategoryName(request.getParameter("categoryName"));
this.dao.mergeCate(researchCate);
System.out.println("merge---success");
return this.listResearchCate(request);
}catch(Exception ex){
ex.printStackTrace();
request.setAttribute("errormsg", ex.getMessage());
return "/error.jsp";
}
}
private String delete(HttpServletRequest request){
try{
String id=request.getParameter("id");
System.out.println("del--id---"+id);
ResearchCate resCate=this.dao.readCate(id);
this.dao.deleteCate(resCate);
System.out.println("delete---success");
return this.listResearchCate(request);
}catch(Exception ex){
ex.printStackTrace();
request.setAttribute("errormsg", ex.getMessage());
return "/error.jsp";
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//TODO Method stub generated by Lomboz
/*if(request.getSession().getAttribute("adminname")==null){
if(request.getParameter("method")!=null
&&!request.getParameter("method").equals("read")) {
response.sendRedirect("adminLogin.jsp");
return;
}
}*/
System.out.println("gjy");
request.setCharacterEncoding("gbk");
String method = request.getParameter("method");
System.out.println("gjy-----"+method);
if (method == null)
method = "";
String result = "";
if(method.equals("listResearchCate"))
result=this.listResearchCate(request);
else if(method.equals("addResearchCate"))
result=this.addResearchCate(request);
else if(method.equals("addResearchCateResult"))
result=this.addResearchCateResult(request);
else if(method.equals("modifyResearchCate"))
result=this.modifyResearchCate(request);
else if(method.equals("modifyResearchCateResult"))
result=this.modifyResearchCateResult(request);
else if(method.equals("delete"))
result=this.delete(request);
else
result=this.listResearchCate(request);
RequestDispatcher dispatcher = request.getSession().getServletContext().getRequestDispatcher(result);
if (dispatcher != null)
dispatcher.forward(request, response);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -