java--学生信息管理3
Posted xanlv
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java--学生信息管理3相关的知识,希望对你有一定的参考价值。
在
http://blog.csdn.net/xanlv/article/details/53158999的基础上重写servlet
学习Hibernate的框架,重写servlet
在utils包中添加BaseServlet(不配置xml),当作一个基类使用
package cn.hncu.utils;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public abstract class BaseServlet extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
doPost(request, response);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
String cmd=request.getParameter("cmd");//streth框架中称action
if(cmd==null||cmd.toString().trim().equals(""))
cmd="execute";//默认方法
//调用子类中名为cmd的方法---利用类反射动态调
try
Method m= this.getClass().getMethod(cmd, HttpServletRequest.class,HttpServletResponse.class);
m.invoke(this, request,response);
catch (Exception e)
throw new RuntimeException(e.getMessage(), e);
public abstract void execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ;
继承BaseServlet,调用BaseServlet中的dopost,doget方法
package cn.hncu.stud.servlet;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.hncu.domain.Book;
import cn.hncu.domain.Stud;
import cn.hncu.stud.service.IStudService;
import cn.hncu.stud.service.StudServiceImpl;
import cn.hncu.utils.BaseServlet;
import cn.hncu.utils.TxProxy3;
public class QueryServlet extends BaseServlet
IStudService service=TxProxy3.getProxy(new StudServiceImpl());
public void query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
System.out.println("service:"+service);
List<Map<String, String>> studs=service.query();
request.setAttribute("studs", studs);
request.getRequestDispatcher("/jsps/show.jsp").forward(request, response);
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
//1收集参数 2组织参数(id字段留到dao中去补)
String name[]=request.getParameterValues("name");
Stud s=new Stud();
s.setName(name[0]);
//图书信息
String books[]=request.getParameterValues("book");
//防护一下 ---价格的防护应该也要写,这里我们偷懒了
if(books==null||books.length<=0)
return;
String prices[]=request.getParameterValues("price");
for(int i=0;i<books.length;i++)
Book b=new Book();
b.setName(books[i]);
b.setPrice(Double.parseDouble(prices[i]));
//※完成两个值对象的“一对多”关系的数据封装
s.getBooks().add(b);//一方
b.setS(s);//多方
//3调用service层
try
service.save(s);
catch (Exception e)
//导向失败页面
@Override
public void execute(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
System.out.println("默认方法");
以上是关于java--学生信息管理3的主要内容,如果未能解决你的问题,请参考以下文章