Hibernate----Web小应用
index.jsp
<body>
<jsp:forward page="/DemoServlet"></jsp:forward>
</body>
DemoServlet.java
public class DemoServlet extends BaseServlet {
private DemoServiceImpl service=new DemoServiceImpl();
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp)
throws Exception {
List<Student> studs=service.qureyAllStudent();
req.getSession().setAttribute("studs", studs);
String path=getInitParameter("queryAll");
resp.sendRedirect(getServletContext().getContextPath()+path);
}
public void del(HttpServletRequest req, HttpServletResponse resp)
throws Exception {
String studId=req.getParameter("studId");
Student stud=new Student();
stud.setStudId(studId);
service.del(stud);
resp.sendRedirect(getServletContext().getContextPath());
}
public void add(HttpServletRequest req, HttpServletResponse resp)
throws Exception {
//******req.setCharacterEncoding("utf-8");这里设置编码没有用
//因为在BaseServlet已经读取过request的参数了;所以request已经被解析了;再在这里进行设置编码是无效的;所以在所有读参数之前设置编码
String studId=req.getParameter("studId");
String studName=req.getParameter("studName");
String age=req.getParameter("studAge");
int studAge=Integer.valueOf(age);
String deptId=req.getParameter("deptId");
Student stud=new Student();
stud.setStudId(studId);
stud.setStudName(studName);
stud.setStudAge(studAge);
stud.setDeptId(deptId);
service.add(stud);
resp.sendRedirect(getServletContext().getContextPath());
}
public void query(HttpServletRequest req, HttpServletResponse resp)
throws Exception {
System.out.println("查询啦-------");
String studId=req.getParameter("studId");
String studName=req.getParameter("studName");
String deptId=req.getParameter("deptId");
Student stud=new Student();
stud.setStudId(studId);
stud.setStudName(studName);
stud.setDeptId(deptId);
List<Student> qstuds=service.query(stud);
req.getSession().setAttribute("qstuds", qstuds);
resp.getWriter().print("1");//这里不要用println;不然ajax会把换行符也接受;
}
public abstract class BaseServlet extends HttpServlet {
private Logger log = Logger.getLogger(BaseServlet.class);
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String cmd = req.getParameter("cmd");
if (null == cmd || cmd.trim().equals("")) {
cmd = "execute";
}
log.debug("调用的方法为:"+cmd);
try {
Method method = this.getClass().getMethod(cmd,
HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this, req, resp);
} catch (NoSuchMethodException e) {
throw new RuntimeException("没有这个方法:" + e.getMessage(), e);
}catch(InvocationTargetException e){
throw new RuntimeException("目标方法执行失败:" + e.getMessage(), e);
}catch(IllegalAccessException e){
throw new RuntimeException("你可能访问了一个私有的方法:" + e.getMessage(), e);
}catch(Exception e){
throw new RuntimeException(e.getMessage(), e);
}
}
public abstract void execute(HttpServletRequest req,
HttpServletResponse resp) throws Exception;
<head>
<title>首页</title>
<style type="text/css">
table{
border: 1px solid gray;
border-collapse: collapse;
width: 60%;
}
td{
border: 1px solid gray;
padding: 5px;
}
</style>
<script type="text/javascript" src="<c:url value='/js/pub.js'/>">
</script>
<script type="text/javascript">
//用来传输路径更根目录的
var path="<c:url value='/'/>";
</script>
<script type="text/javascript">
function query(){//利用ajax实现页刷新技术;不是数据校验;
//利用ajax能接受后台数据的功能来操纵前端页面显示
var studId=document.getElementsByName("studId")[1].value;
studId=studId.trim();
//alert(studId);
var studName=document.getElementsByName("studName")[1].value;
studName=studName.trim();
var deptId=document.getElementsByName("deptId")[1].value;
deptId=deptId.trim();
var url=path+"DemoServlet?cmd=query";
var params="studId="+studId+"&studName="+studName+"&deptId="+deptId;
var ajax=new Ajax();
ajax.post(url,params,function(data){
if(data==1){
//alert(data);测试
//window.parent.window.location.href=path;
var frm=document.getElementById("frm");
//将这个页面导入frm 中
frm.src=path+"jsps/qlist.jsp";//这个页面最终会在frm中国显示
frm.style.display="block";//设置为可见
}
});
}
</script>
</head>
<body><table>
<tr><td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>院系</td>
<td>操作</td></tr>
<c:forEach items="${studs}" var="stud">
<tr>
<td>${stud.studId}</td>
<td>${stud.studName}</td>
<td>${stud.studAge}</td>
<td>${stud.deptId}</td>
<td><a href="<c:url value='/DemoServlet?cmd=del&studId=${stud.studId}'/>">删除</a></td>
</tr>
</c:forEach>
</table>
<hr>
<h2>添加学生信息</h2>
<form action="<c:url value='/DemoServlet?cmd=add'/>" method="post">
<table>
<tr>
<td>学号</td>
<td> <input type="text" name="studId"/> </td>
</tr>
<tr>
<td>姓名</td>
<td> <input type="text" name="studName"/> </td>
</tr>
<tr>
<td>学院编号</td>
<td> <input type="text" name="deptId"/> </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="添加"/>
</td>
</tr>
</table>
</form>
<hr>
<h2>学生信息查询</h2>
<table>
<tr>
<td>学号</td>
<td> <input type="text" name="studId"/> </td>
</tr>
<tr>
<td>姓名</td>
<td> <input type="text" name="studName"/> </td>
</tr>
<tr>
<td>学院编号</td>
<td> <input type="text" name="deptId"/> </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" value="查询" οnclick="query();"/>
</td>
</tr>
</table>
<br/><br/>
<!-- 隐藏帧技术 -->
<iframe id="frm" style="display:none;width:50%;border-width:0px " >
</iframe>
<br/><br/><br/></body>
pub.js(Ajax工具
String.prototype.trim=function(){
var p = /^\s*/;
var str = this.replace(p,"");
p = /\s*$/;
str = str.replace(p,"");
return str;
};
function Ajax(){
this.getHttp=function(){//生成ajax对象
if(window.XMLHttpRequest){
return new XMLHttpRequest();
}else{
return new ActiveXObject("Microsoft.XMLHttp");
}
};
this.post=function(url,params,func,failure){
var http = this.getHttp();//获取XHR对象
http.open("POST",url,true);
http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
http.onreadystatechange=function(){
if(http.readyState==4){
if(http.status==200){
//接受后台参数
var txt = http.responseText;
//调用用户的成功方法
func(txt);
}else{
//如果存在失败方法则,如果失败,则调用用户的失败方法
if(failure){
failure(http.status,http);
}
}
}
};
if(params){
http.send(params);
}else{
http.send();
}
};
})
qlist.jsp
<body><table><tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>院系</td>
</tr>
<c:forEach items="${qstuds}" var="stud">
<tr> <td>${stud.studId}</td>
<td>${stud.studName}</td>
<td>${stud.studAge}</td>
<td>${stud.deptId}</td>
</tr>
</c:forEach></table></body>
index.jsp
<body>
<jsp:forward page="/DemoServlet"></jsp:forward>
</body>
DemoServlet.java
public class DemoServlet extends BaseServlet {
private DemoServiceImpl service=new DemoServiceImpl();
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp)
throws Exception {
List<Student> studs=service.qureyAllStudent();
req.getSession().setAttribute("studs", studs);
String path=getInitParameter("queryAll");
resp.sendRedirect(getServletContext().getContextPath()+path);
}
public void del(HttpServletRequest req, HttpServletResponse resp)
throws Exception {
String studId=req.getParameter("studId");
Student stud=new Student();
stud.setStudId(studId);
service.del(stud);
resp.sendRedirect(getServletContext().getContextPath());
}
public void add(HttpServletRequest req, HttpServletResponse resp)
throws Exception {
//******req.setCharacterEncoding("utf-8");这里设置编码没有用
//因为在BaseServlet已经读取过request的参数了;所以request已经被解析了;再在这里进行设置编码是无效的;所以在所有读参数之前设置编码
String studId=req.getParameter("studId");
String studName=req.getParameter("studName");
String age=req.getParameter("studAge");
int studAge=Integer.valueOf(age);
String deptId=req.getParameter("deptId");
Student stud=new Student();
stud.setStudId(studId);
stud.setStudName(studName);
stud.setStudAge(studAge);
stud.setDeptId(deptId);
service.add(stud);
resp.sendRedirect(getServletContext().getContextPath());
}
public void query(HttpServletRequest req, HttpServletResponse resp)
throws Exception {
System.out.println("查询啦-------");
String studId=req.getParameter("studId");
String studName=req.getParameter("studName");
String deptId=req.getParameter("deptId");
Student stud=new Student();
stud.setStudId(studId);
stud.setStudName(studName);
stud.setDeptId(deptId);
List<Student> qstuds=service.query(stud);
req.getSession().setAttribute("qstuds", qstuds);
resp.getWriter().print("1");//这里不要用println;不然ajax会把换行符也接受;
}
}
public abstract class BaseServlet extends HttpServlet {
private Logger log = Logger.getLogger(BaseServlet.class);
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String cmd = req.getParameter("cmd");
if (null == cmd || cmd.trim().equals("")) {
cmd = "execute";
}
log.debug("调用的方法为:"+cmd);
try {
Method method = this.getClass().getMethod(cmd,
HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this, req, resp);
} catch (NoSuchMethodException e) {
throw new RuntimeException("没有这个方法:" + e.getMessage(), e);
}catch(InvocationTargetException e){
throw new RuntimeException("目标方法执行失败:" + e.getMessage(), e);
}catch(IllegalAccessException e){
throw new RuntimeException("你可能访问了一个私有的方法:" + e.getMessage(), e);
}catch(Exception e){
throw new RuntimeException(e.getMessage(), e);
}
}
public abstract void execute(HttpServletRequest req,
HttpServletResponse resp) throws Exception;
}
<head>
<title>首页</title>
<style type="text/css">
table{
border: 1px solid gray;
border-collapse: collapse;
width: 60%;
}
td{
border: 1px solid gray;
padding: 5px;
}
</style>
<script type="text/javascript" src="<c:url value='/js/pub.js'/>">
</script>
<script type="text/javascript">
//用来传输路径更根目录的
var path="<c:url value='/'/>";
</script>
<script type="text/javascript">
function query(){//利用ajax实现页刷新技术;不是数据校验;
//利用ajax能接受后台数据的功能来操纵前端页面显示
var studId=document.getElementsByName("studId")[1].value;
studId=studId.trim();
//alert(studId);
var studName=document.getElementsByName("studName")[1].value;
studName=studName.trim();
var deptId=document.getElementsByName("deptId")[1].value;
deptId=deptId.trim();
var url=path+"DemoServlet?cmd=query";
var params="studId="+studId+"&studName="+studName+"&deptId="+deptId;
var ajax=new Ajax();
ajax.post(url,params,function(data){
if(data==1){
//alert(data);测试
//window.parent.window.location.href=path;
var frm=document.getElementById("frm");
//将这个页面导入frm 中
frm.src=path+"jsps/qlist.jsp";//这个页面最终会在frm中国显示
frm.style.display="block";//设置为可见
}
});
}
</script>
</head>
<body><table>
<tr><td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>院系</td>
<td>操作</td></tr>
<c:forEach items="${studs}" var="stud">
<tr>
<td>${stud.studId}</td>
<td>${stud.studName}</td>
<td>${stud.studAge}</td>
<td>${stud.deptId}</td>
<td><a href="<c:url value='/DemoServlet?cmd=del&studId=${stud.studId}'/>">删除</a></td>
</tr>
</c:forEach>
</table>
<hr>
<h2>添加学生信息</h2>
<form action="<c:url value='/DemoServlet?cmd=add'/>" method="post">
<table>
<tr>
<td>学号</td>
<td> <input type="text" name="studId"/> </td>
</tr>
<tr>
<td>姓名</td>
<td> <input type="text" name="studName"/> </td>
</tr>
<tr>
<td>学院编号</td>
<td> <input type="text" name="deptId"/> </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="添加"/>
</td>
</tr>
</table>
</form>
<hr>
<h2>学生信息查询</h2>
<table>
<tr>
<td>学号</td>
<td> <input type="text" name="studId"/> </td>
</tr>
<tr>
<td>姓名</td>
<td> <input type="text" name="studName"/> </td>
</tr>
<tr>
<td>学院编号</td>
<td> <input type="text" name="deptId"/> </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" value="查询" οnclick="query();"/>
</td>
</tr>
</table>
<br/><br/>
<!-- 隐藏帧技术 -->
<iframe id="frm" style="display:none;width:50%;border-width:0px " >
</iframe>
<br/><br/><br/></body>
pub.js(Ajax工具
String.prototype.trim=function(){
var p = /^\s*/;
var str = this.replace(p,"");
p = /\s*$/;
str = str.replace(p,"");
return str;
};
function Ajax(){
this.getHttp=function(){//生成ajax对象
if(window.XMLHttpRequest){
return new XMLHttpRequest();
}else{
return new ActiveXObject("Microsoft.XMLHttp");
}
};
this.post=function(url,params,func,failure){
var http = this.getHttp();//获取XHR对象
http.open("POST",url,true);
http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
http.onreadystatechange=function(){
if(http.readyState==4){
if(http.status==200){
//接受后台参数
var txt = http.responseText;
//调用用户的成功方法
func(txt);
}else{
//如果存在失败方法则,如果失败,则调用用户的失败方法
if(failure){
failure(http.status,http);
}
}
}
};
if(params){
http.send(params);
}else{
http.send();
}
};
})
qlist.jsp
<body><table><tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>院系</td>
</tr>
<c:forEach items="${qstuds}" var="stud">
<tr> <td>${stud.studId}</td>
<td>${stud.studName}</td>
<td>${stud.studAge}</td>
<td>${stud.deptId}</td>
</tr>
</c:forEach></table></body>