jsp出现500问题
代码:<%@pagelanguage="java"import="java.sql.*"contentType="text/html;charset=gb2312"pag...
代码:
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=gb2312" pageEncoding="gbk"%>
<html>
<head>
<title>添加图书信息</title>
</head>
<body>
<center>添加图书信息
<form action="add.jsp" method="post">
<table width="291" border="1" height="50">
<tr>
<td>书号</td>
<td><input type="text" name="id"></td></tr>
<tr>
<td>书名</td>
<td><input type="text" name="bookname"></td></tr>
<tr>
<td>作者</td>
<td><input type="text" name="author"></td></tr>
<tr>
<td>价格</td>
<td><input type="text" name="price">元</td></tr><p>
<td colspan="2" align="center">
<input type="submit" value="添加"><input type="reset" value="重置">
</td>
</table>
</form>
<%
request.setCharacterEncoding("gb2312");
String id=request.getParameter("id");
String bookname=request.getParameter("bookname");
String author=request.getParameter("author");
String price=request.getParameter("price");
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","123456");
Statement stmt=conn.createStatement();
String s="insert into bookinfo(id,bookname,author,price)values("+id+",'"+bookname+"','"+author+"',"+price+")";
int i=stmt.executeUpdate(s);
if(i==1){
out.println("<script language='javascript'>alert('添加成功,单击确定跳转到主页面!')</script>");
}
response.setHeader("refresh","1;url=index.jsp");
stmt.close();
conn.close();
%>
</body>
</html>
出现500问题:
org.apache.jasper.JasperException: Exception in JSP: /add.jsp:37
34: Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","123456");
35: Statement stmt=conn.createStatement();
36: String s="insert into bookinfo(id,bookname,author,price)values("+id+",'"+bookname+"','"+author+"',"+price+")";
37: int i=stmt.executeUpdate(s);
38: if(i==1){
39: out.println("<script language='javascript'>alert('添加成功,单击确定跳转到主页面!')</script>");
40: }
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:489)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) 展开
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=gb2312" pageEncoding="gbk"%>
<html>
<head>
<title>添加图书信息</title>
</head>
<body>
<center>添加图书信息
<form action="add.jsp" method="post">
<table width="291" border="1" height="50">
<tr>
<td>书号</td>
<td><input type="text" name="id"></td></tr>
<tr>
<td>书名</td>
<td><input type="text" name="bookname"></td></tr>
<tr>
<td>作者</td>
<td><input type="text" name="author"></td></tr>
<tr>
<td>价格</td>
<td><input type="text" name="price">元</td></tr><p>
<td colspan="2" align="center">
<input type="submit" value="添加"><input type="reset" value="重置">
</td>
</table>
</form>
<%
request.setCharacterEncoding("gb2312");
String id=request.getParameter("id");
String bookname=request.getParameter("bookname");
String author=request.getParameter("author");
String price=request.getParameter("price");
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","123456");
Statement stmt=conn.createStatement();
String s="insert into bookinfo(id,bookname,author,price)values("+id+",'"+bookname+"','"+author+"',"+price+")";
int i=stmt.executeUpdate(s);
if(i==1){
out.println("<script language='javascript'>alert('添加成功,单击确定跳转到主页面!')</script>");
}
response.setHeader("refresh","1;url=index.jsp");
stmt.close();
conn.close();
%>
</body>
</html>
出现500问题:
org.apache.jasper.JasperException: Exception in JSP: /add.jsp:37
34: Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","123456");
35: Statement stmt=conn.createStatement();
36: String s="insert into bookinfo(id,bookname,author,price)values("+id+",'"+bookname+"','"+author+"',"+price+")";
37: int i=stmt.executeUpdate(s);
38: if(i==1){
39: out.println("<script language='javascript'>alert('添加成功,单击确定跳转到主页面!')</script>");
40: }
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:489)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) 展开
3个回答
展开全部
首先你要理解一点:jsp中的程序代码在页面加载的时候就被加载执行,所以,其中脚本程序也就是<%%>中的代码是有上至下,左至右,逐字加载运行,所以,你上述代码,既然想用表单提交,我想问一下,你的表单提交到add.jsp中,add.jsp代码呢?还有,你的这个页面,在表单下面还有脚本程序代码,不是找错吗?这样,在此页面加载的时候,这堆代码不就运行加载了吗?而其中的与请求有关的request对象这一片代码就是空指针,也就是它获取的“id”......的属性值都是null(空)。
第二:既然下面的控制台给你提示在-----37: int i=stmt.executeUpdate(s); 行有错,那按常理一般认为是sql语句出错,或者要添加记录的语句跟数据库设计冲突。。。
第二:既然下面的控制台给你提示在-----37: int i=stmt.executeUpdate(s); 行有错,那按常理一般认为是sql语句出错,或者要添加记录的语句跟数据库设计冲突。。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询