jsp程序出错,求助(基于hibernate+Eclipse+MyEclipse)
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%><%Stringpath=request....
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="cn.com.twm.*" %>
<%@page import="org.hibernate.*" %>
<!-- 导入需要的包 -->
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Ins.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//判断id有没有值,如果有,则通过request对象获取多个请求参数
if (request.getParameter("id")!=null){
int id =Integer.parseInt(request.getParameter("id"));
String name =request.getParameter("name");
String department=request.getParameter("department");
Double salary=Double.parseDouble(request.getParameter("salary"));
try {
//根据用户请求信息构建一个持久化对象
Transaction tx=null;
Employee em=new Employee();
em.setId(id);
em.setName(name);
em.setDepartment(department);
em.setSalary(salary);
//打开一个hibernate会话
Session Hsession=HibernateUtil.currentSession();
tx=Hsession.beginTransaction;//调用session方法的save方法将持久化对象保存到数据库
Hsession.save(em);
tx.commit();
HibernateUtil.closeSession();
//关闭session
}catch(Exception e){
System.out.println("添加失败"+e);}
}
%>
<!--提交form表单元素 -->
<form action="Ins.jsp" method="post" name="forml" target="_self">
<p>ID:<input name="id" type="text" id="id">
</p>
<p>姓名:<input name="name" type="text" id="name">
</p>
<p>部门:<input name="department" type="text" id="department"></p>
<p>
薪水:<input name="salary" type="text" id="salary"></p>
<p> <input type="submit" name="submit" value="提交">
</p>
</form>
</body>
</html>
Severity and Description Path Resource Location Creation Time Id
提示错误原因:Hsession.beginTransaction cannot be resolved or is not a field hiber/WebRoot Ins.jsp line 48 1249807186375 62
还是不能运行
Ins.jsp这个页面可以填写表单,但是提交表单就出错,下面语句有问题
Session Hsession =HibernateUtil.currentSession();
org.apache.jasper.JasperException: An exception occurred processing JSP page /Ins.jsp at line 50
47: em.setDepartment(department);
48: em.setSalary(salary);
49: //镓揿紑涓�釜hibernate浼氲瘽
50: Session Hsession =HibernateUtil.currentSession();
51: tx =Hsession.beginTransaction();//璋幂敤session鏂规硶镄剆ave鏂规硶灏嗘寔涔呭寲瀵硅薄淇濆瓨鍒版暟鎹�簱
52: Hsession.save(em);
53: tx.commit();
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 展开
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="cn.com.twm.*" %>
<%@page import="org.hibernate.*" %>
<!-- 导入需要的包 -->
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Ins.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//判断id有没有值,如果有,则通过request对象获取多个请求参数
if (request.getParameter("id")!=null){
int id =Integer.parseInt(request.getParameter("id"));
String name =request.getParameter("name");
String department=request.getParameter("department");
Double salary=Double.parseDouble(request.getParameter("salary"));
try {
//根据用户请求信息构建一个持久化对象
Transaction tx=null;
Employee em=new Employee();
em.setId(id);
em.setName(name);
em.setDepartment(department);
em.setSalary(salary);
//打开一个hibernate会话
Session Hsession=HibernateUtil.currentSession();
tx=Hsession.beginTransaction;//调用session方法的save方法将持久化对象保存到数据库
Hsession.save(em);
tx.commit();
HibernateUtil.closeSession();
//关闭session
}catch(Exception e){
System.out.println("添加失败"+e);}
}
%>
<!--提交form表单元素 -->
<form action="Ins.jsp" method="post" name="forml" target="_self">
<p>ID:<input name="id" type="text" id="id">
</p>
<p>姓名:<input name="name" type="text" id="name">
</p>
<p>部门:<input name="department" type="text" id="department"></p>
<p>
薪水:<input name="salary" type="text" id="salary"></p>
<p> <input type="submit" name="submit" value="提交">
</p>
</form>
</body>
</html>
Severity and Description Path Resource Location Creation Time Id
提示错误原因:Hsession.beginTransaction cannot be resolved or is not a field hiber/WebRoot Ins.jsp line 48 1249807186375 62
还是不能运行
Ins.jsp这个页面可以填写表单,但是提交表单就出错,下面语句有问题
Session Hsession =HibernateUtil.currentSession();
org.apache.jasper.JasperException: An exception occurred processing JSP page /Ins.jsp at line 50
47: em.setDepartment(department);
48: em.setSalary(salary);
49: //镓揿紑涓�釜hibernate浼氲瘽
50: Session Hsession =HibernateUtil.currentSession();
51: tx =Hsession.beginTransaction();//璋幂敤session鏂规硶镄剆ave鏂规硶灏嗘寔涔呭寲瀵硅薄淇濆瓨鍒版暟鎹�簱
52: Hsession.save(em);
53: tx.commit();
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 展开
2个回答
展开全部
你的事务开启有问题
一般是这样写:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx= session.beginTransaction();
session.save(user1);
tx.commit();
session.close();
sessionFactory.close();
一般是这样写:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx= session.beginTransaction();
session.save(user1);
tx.commit();
session.close();
sessionFactory.close();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询