问题出在哪里?代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>处理合同添加数据</title>
</head>
<body>
<%
String clientName=new String(request.getParameter("clientName").getBytes("ISO-8859-1"),"UTF-8");
String contactName=new String(request.getParameter("contactName").getBytes("ISO-8859-1"),"UTF-8");
String contactContents=new String(request.getParameter("contactContents").getBytes("ISO-8859-1"),"UTF-8");
String contactStart=new String(request.getParameter("contactStart").getBytes("ISO-8859-1"),"UTF-8");
String contactEnd=new String(request.getParameter("contactEnd").getBytes("ISO-8859-1"),"UTF-8");
String staffName=new String(request.getParameter("staffName").getBytes("ISO-8859-1"),"UTF-8");
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/eims?useUnicode=true&characterEncoding=gbk";
String user="root";
String password="";
Connection con=DriverManager.getConnection(url,user,password);
Statement st=con.createStatement();
String sql="insert into contact values('"+clientName+"','"+contactName+"','"+contactContents+"','"+contactStart+"','"+contactEnd+"','"+staffName+"')";
st.executeUpdate(sql);
response.sendRedirect("http://localhost:8080/EIMS/contactManage/lookContact.jsp");
}
catch(Exception e){
e.printStackTrace();
}
finally{
st.close();
con.close();
}
%>
</body>
</html>
错误信息:
HTTP Status 500 - Unable to compile class for JSP:
type Exception report
message Unable to compile class for JSP:
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 34 in the jsp file: /contactManage/addContactCheck.jsp
st cannot be resolved
31: e.printStackTrace();
32: }
33: finally{
34: st.close();
35: con.close();
36: }
37: %>
An error occurred at line: 35 in the jsp file: /contactManage/addContactCheck.jsp
con cannot be resolved
32: }
33: finally{
34: st.close();
35: con.close();
36: }
37: %>
38: </body>
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:199)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:446)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:405)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:349)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.9 logs.
------解决方案--------------------
作用范围 问题 。最好数据库连接封装到一个共通的jsp
------解决方案--------------------
变量作用域问题,不声明全局变量也可以,你刚出错是因为你在try块里面定义的局部变量,finally是访问不到的,所以要定义在try块外面。还有这个全局变量赋null值应该是一种好的编程习惯,引用类型的全局变量可以默认初始化为null,但局部变量则必须手动初始化。
------解决方案--------------------
jsp文件编译错误,你的jsp代码有明显错误,ide没提示么?
把st,con提出来定义到try块上面就好了
------解决方案--------------------
把你的st和conn定义在try代码块外面就行了,你想要在finally中把连接关闭,但是现在关闭的时候找不到相应的对象,所以就不能解析成为servlet,就报错了,主要是作用域的问题,注意就行了
------解决方案--------------------
这样写会有并发问题,因为容器只会对servlet实例化一次,和static Connection con;static Statement st;这样写基本上有同样的效果
------解决方案--------------------
把 Connection 和 Statement 定义到try块的上面定义为全局变量。还有你最好在finally块内关闭con与st的时候加判断。判断con和st是否为空,为空就不用关闭了,否则还会出错。希望采纳