我正在创建一个允许管理员管理菜单项的网页。对于删除部分,它是成功的…但是当我想插入一个新菜单时出现错误。
我使用phpmyadmin作为我的数据库
当我运行netbeans项目时出现这个错误
HTTP状态500-
type: Exception report
description:服务器遇到一个内部错误(),阻止它完成这个请求。
exception: org.apache.jasper.JasperException: java.lang.IllegalArgumentException:参数类型不匹配
根本原因: java.lang.IllegalArgumentException:参数类型不匹配
全栈跟踪
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.jasper.runtime.JspRuntimeLibrary.handleSetProperty(JspRuntimeLibrary.java:672)
at org.apache.jsp.confirmadd_jsp._jspService(confirmadd_jsp.java:90)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
managemenu.jsp
<h3>Add new menu</h3>
<div>
<form method="post" action="confirmadd.jsp">
<table>
<tr><td>Food ID</td><td>:</td><td><input type="text" name="food_id" placeholder="food ID"/></td></tr>
<tr><td>Food Name</td><td>:</td><td><input type="text" name="food_name" placeholder="food name"/></td></tr>
<tr><td>Food Description</td><td>:</td><td><input type="text" name="food_description" placeholder="food desc"/></td></tr>
<tr><td>Food Price</td><td>: </td><td><input type="text" name="food_price" placeholder="food price"/></td></tr>
</table>
<input type="submit" value="Submit"/><input type="reset" value="Reset"/>
</form>
confirmadd.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ page import="java.util.*" %>
<jsp:useBean id="newmenu" class="restaurantBean.menu" scope="session"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="css/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<title>Confirm Add Food</title>
</head>
<body>
<center><br><h1>Restaurant E-menu and Reservation System</h1><br><br>
<a href="main.jsp">Home</a>
<a href="managemenu.jsp">Manage Menu</a>
<a href="updatestock.jsp">Update stock</a>
<div class="content">
<h1>Confirm Food</h1>
<jsp:setProperty name="newmenu" property="food_id" value='<%=request.getParameter("food_id")%>'/>
<jsp:setProperty name="newmenu" property="food_name" value="<%=request.getParameter("food_name")%>"/>
<jsp:setProperty name="newmenu" property="food_description" value='<%=request.getParameter("food_description")%>'/>
<jsp:setProperty name="newmenu" property="food_price" value='<%=request.getParameter("food_price")%>'/>
<table id="form">
<tr><th colspan="3">Food Details</th></tr>
<tr><td>Food ID</td><td>:</td><td><jsp:getProperty name="newmenu" property="food_id"/></td></tr>
<tr><td>Food Name</td><td>:</td><td><jsp:getProperty name="newmenu" property="food_name"/></td></tr>
<tr><td>Food Description</td><td>:</td><td><jsp:getProperty name="newmenu" property="food_description"/></td></tr>
<tr><td>Food Price</td><td>:</td><td>RM<jsp:getProperty name="newmenu" property="food_price"/></td></tr>
<tr><td colspan="3"><input type="button" value="proceed" onclick="location.href='./addmenu'"/>
<input type="button" value="Back" onClick="history.go(-1);return true;"></td></tr>
</table>
</div>
</div>
<jsp:include page="/WEB-INF/includes/footer.jsp" />
</div>
</body>
</html>
menu.java
package restaurantBean;
public class menu {
private String food_description;
private String food_id;
private String food_name;
private double food_price;
public void setFood_description(String a){
food_description=a;
}
public String getFood_description(){
return food_description;
}
public void setFood_id(String b){
food_id=b;
}
public String getFood_id(){
return food_id;
}
public void setFood_price(double c){
food_price=c;
}
public double getFood_price(){
return food_price;
}
public void setFood_name(String d){
food_name=d;
}
public String getFood_name(){
return food_name;
}
}
addmenu.java
package restaurantPackages;
import restaurantBean.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet(name = "addmenu", urlPatterns = {"/addmenu"})
public class addmenu extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver");
HttpSession session=request.getSession();
menu m=(menu) session.getAttribute("newmenu");
String url="jdbc:mysql://localhost:3306/restaurant";
String username="root";
String password="";
Connection con=DriverManager.getConnection(url,username,password);
Statement statement=con.createStatement();
//ResultSet result=statement.executeQuery("select food_id from food where food_name='"+m.getFood_name().trim()+"'");
//int c = 0;
//while(result.next()){
//c=result.getInt(1);
//}
PreparedStatement stmt=con.prepareStatement("insert into food (food_id, food_name, food_description, food_price) values (?,?,?,?)");
stmt.setString(1,m.getFood_id());
stmt.setString(2,m.getFood_name());
stmt.setString(3,m.getFood_description());
stmt.setDouble(4,m.getFood_price());
stmt.executeUpdate();
stmt.close();
con.close();
menu food=new menu();
session.setAttribute("food",food);
RequestDispatcher dis=request.getRequestDispatcher("/WEB-INF/addsuccess.jsp");
dis.forward(request,response);
}
catch(java.lang.ClassNotFoundException e){
out.println("ClassNotFoundException:"+e.getMessage());
}catch(SQLException e){
out.println("SQLState: " + e.getSQLState());
out.println("Message: " + e.getMessage());
out.println("Vendor: " + e.getErrorCode());
}
finally {
out.close();
}
}
addsuccess.jsp
<div class="content">
<tr><td>Successfully add food ID = ${food.food_id} from restaurant</td></tr>
<table id="form">
<tr><th colspan="3">New Added Food Details</th></tr>
<tr><td>Food ID</td><td>:</td><td>${food.food_id}</td></tr>
<tr><td>Food Name</td><td>:</td><td>${food.food_name}</td></tr>
<tr><td>Food Description</td><td>:</td><td>${food.food_description}</td></tr>
<tr><td>Food Price</td><td>:</td><td>${food.food_price}</td></tr>
</table>
<tr><td><input type="button" value="Continue Manage Menu" onclick="location.href='managemenu.jsp'"/></td></tr>
<tr><td><input type="button" value="View Menu" onclick="location.href='main.jsp'"/> </td></tr>
由于没有完整的堆栈跟踪,我猜问题在这里:
<jsp:setProperty name="newmenu" property="food_price" value='<%=request.getParameter("food_price")%>'/>
food_price字段是一个double,也许你的web表单上的价格格式不能转换为double(只是为了测试改变food_price类型为String,看看会发生什么)。并在这里复制更多的堆栈跟踪,这样更容易识别问题。