- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在我的项目中实现 CRUD 功能。我能够在 jsp 上显示数据库表,但是当我尝试删除条目时,出现此错误:(我看到 servlet 中的一行有问题,但不明白为什么它不能检索 bookingID。):
Warning: StandardWrapperValve[UserController]: Servlet.service() for servlet UserController threw exception
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.parseInt(Integer.java:615)
at com.daniel.controller.UserController.doGet(UserController.java:34)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)
预订.java
package com.daniel.model;
public class Booking {
private int bookingID = -1;
private String bookingDate = null;
private String reasoning = null;
private String bookingStatus = null;
private String bookingReview = null;
public int getBookingID() {
return bookingID;
}
public void setBookingID(int bookingID) {
this.bookingID = bookingID;
}
public String getBookingDate() {
return bookingDate;
}
public void setBookingDate(String bookingDate) {
this.bookingDate = bookingDate;
}
public String getReasoning() {
return reasoning;
}
public void setReasoning(String reasoning) {
this.reasoning = reasoning;
}
public String getBookingStatus() {
return bookingStatus;
}
public void setBookingStatus(String bookingStatus) {
this.bookingStatus = bookingStatus;
}
public String getBookingReview() {
return bookingReview;
}
public void setBookingReview(String bookingReview) {
this.bookingReview = bookingReview;
}
@Override
public String toString() {
return "Booking [bookingID=" + bookingID + ", bookingDate=" + bookingDate
+ ", reasoning=" + reasoning + ", bookingStatus=" + bookingStatus + ", bookingReview="
+ bookingReview + "]";
}
}
BookingDao.java
package com.daniel.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.daniel.model.Booking;
import com.daniel.util.DbUtil;
public class BookingDao {
private Connection connection;
public BookingDao() {
connection = DbUtil.getConnection();
}
public void addUser(Booking booking) {
try {
PreparedStatement preparedStatement = connection
.prepareStatement("insert into booking(booking_id,booking_date,reasoning,booking_status) values (?, ?, ?, 'Upcoming' )");
// Parameters start with 1
preparedStatement.setInt(1, booking.getBookingID());
preparedStatement.setString(2, booking.getBookingDate());
preparedStatement.setString(3, booking.getReasoning());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteUser(int bookingID) {
try {
PreparedStatement preparedStatement = connection
.prepareStatement("delete from booking where booking_id=?");
// Parameters start with 1
preparedStatement.setInt(1, bookingID);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateUser(Booking booking) {
try {
PreparedStatement preparedStatement = connection
.prepareStatement("update booking set booking_date=?, reasoning=?" +
"where booking_id=?");
// Parameters start with 1
preparedStatement.setString(1, booking.getBookingDate());
preparedStatement.setString(2, booking.getReasoning());
preparedStatement.setInt(3, booking.getBookingID());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Booking> getAllUsers() {
List<Booking> bookings = new ArrayList<Booking>();
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from booking");
while (rs.next()) {
Booking booking = new Booking();
booking.setBookingID(rs.getInt(1));
booking.setBookingDate(rs.getString(2));
booking.setReasoning(rs.getString(3));
booking.setBookingStatus(rs.getString(4));
booking.setBookingReview(rs.getString(5));
bookings.add(booking);
System.out.println(booking);
}
} catch (SQLException e) {
e.printStackTrace();
}
return bookings;
}
public Booking getUserById(int bookingID) {
Booking booking = new Booking();
try {
PreparedStatement preparedStatement = connection.
prepareStatement("select * from booking where booking_id=?");
preparedStatement.setInt(1, bookingID);
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) {
booking.setBookingID(rs.getInt(1));
booking.setBookingDate(rs.getString(2));
booking.setReasoning(rs.getString(3));
booking.setBookingStatus(rs.getString(4));
booking.setBookingReview(rs.getString(5));
}
} catch (SQLException e) {
e.printStackTrace();
}
return booking;
}
}
UserController.java
package com.daniel.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.daniel.dao.BookingDao;
import com.daniel.model.Booking;
public class UserController extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String INSERT_OR_EDIT = "/user.jsp";
private static String LIST_USER = "/listUser.jsp";
private BookingDao dao;
public UserController () {
super();
dao = new BookingDao();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String forward="";
String action = request.getParameter("action");
if (action.equalsIgnoreCase("delete")){
System.out.println(request.getParameter("bookingID"));
int bookingID = Integer.parseInt(request.getParameter("bookingID"));
dao.deleteUser(bookingID);
forward = LIST_USER;
request.setAttribute("users", dao.getAllUsers());
} else if (action.equalsIgnoreCase("edit")){
forward = INSERT_OR_EDIT;
int bookingID = Integer.parseInt(request.getParameter("bookingID"));
Booking booking = dao.getUserById(bookingID);
request.setAttribute("booking", booking);
} else if (action.equalsIgnoreCase("listUser")){
forward = LIST_USER;
request.setAttribute("users", dao.getAllUsers());
} else {
forward = INSERT_OR_EDIT;
}
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Booking booking = new Booking();
booking.setBookingDate(request.getParameter("bookingDate"));
booking.setReasoning(request.getParameter("reasoning"));
String bookingID = request.getParameter("bookingID");
if(bookingID == null || bookingID.isEmpty())
{
dao.addUser(booking);
}
else
{
booking.setBookingID(Integer.parseInt(bookingID));
dao.updateUser(booking);
}
RequestDispatcher view = request.getRequestDispatcher(LIST_USER);
request.setAttribute("users", dao.getAllUsers());
view.forward(request, response);
}
}
listUser.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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=EUC-KR">
<title>Show All Users</title>
</head>
<body>
<table border=1>
<thead>
<tr>
<th>User Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>DOB</th>
<th>Email</th>
<th colspan=2>Action</th>
</tr>
</thead>
<tbody>
<c:forEach items="${users}" var="booking">
<tr>
<td><c:out value="${booking.bookingID}" /></td>
<td><c:out value="${booking.bookingDate}" /></td>
<td><c:out value="${booking.reasoning}" /></td>
<td><c:out value="${booking.bookingStatus}" /></td>
<td><c:out value="${booking.bookingReview}" /></td>
<td><a href="UserController?action=edit&userId=<c:out value="${booking.bookingID}"/>">Update</a></td>
<td><a href="UserController?action=delete&userId=<c:out value="${booking.bookingID}"/>">Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<p><a href="UserController?action=insert">Add User</a></p>
</body>
</html>
最佳答案
您要删除的链接使用以下 URL:
UserController?action=delete&userId=<c:out value="${booking.bookingID}"/>
你的代码使用
request.getParameter("bookingID")
很明显,bookingID 参数为空,因为 URL 中没有这样的参数。该参数名为 userId
。
请注意,使用链接删除某些内容是一个非常非常糟糕的主意。任何人都可以向您发送包含删除 URL 的链接,如果您单击它:砰!你删除了一个用户。
更糟糕的是:如果该页面被公开,GoogleBot(以及所有其他搜索引擎机器人)将点击页面中的每个链接来抓取您的网站,从而删除所有用户。
使用表单,通过 POST 提交。
关于java - request.getParameter 正在检索 'null',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35829513/
我有一个加号/减号按钮,希望用户不能选择超过 20 个但不知道如何让它工作。我尝试使用 min="1"max="5 属性,但它们不起作用。这是我的代码和一个 fiddle 链接。https://jsf
我正在尝试复制顶部底部图,如示例 here但它没有正确渲染(紫色系列有 +ve 和 -ve 值,绿色为负值)留下杂乱的人工制品。我也在努力创建一个玩具示例来复制这个问题,所以我希望尽管我缺乏数据,但有
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 已关闭 6 年前。 社区去年审查了是
这个问题在这里已经有了答案: Adding two positive integers gives negative answer.Why? (4 个答案) 关闭 5 年前。 我遇到了一个奇怪的问题
有谁知道如何将字符串值类型 -4,5 或 5,4 转换为 double -4.5 或 5.4? 最佳答案 只需使用 Double.parseDouble(Locale, String); 糟糕,我很困
我正在尝试根据 TextBlob 分类插入一个仅包含“正”或“负”字符串的新数据框列:对于我的 df 的第一行,结果是 ( pos , 0.75, 0.2499999999999997)我想要' 正
我对 VBA 非常陌生,无法理解如何在一个循环中完成 2 个任务。我非常感谢您的帮助。 我已经能够根据第 3 列中的数据更改第 2 列中的数值,但我不明白如何将负值的字体更改为红色。 表格的大小每月都
欢迎, 我正在使用 jquery 通过 POST 发送表单。 这就是我获得值(value)的方式。 var mytext = $("#textareaid").val(); var dataStrin
double d = 0; // random decimal value with it's integral part within the range of Int32 and always p
我有这个字符串: var a='abc123#xyz123'; 我想构建 2 个正则表达式替换函数: 1) 用 '*' 替换所有确实有 future '#'的字符(不包括'#') 所以结果应该是这样的
我正在使用 DialogFragment。当用户从 Gmail 平板电脑应用程序的屏幕与下面示例图片中的编辑文本进行交互时,我希望正面和负面按钮保持在键盘上方。 在我的尝试中不起作用,这是我的 Dia
从组装艺术一书中,我复制了这句话: In the two’s complement system, the H.O. bit of a number is a sign bit. If the H.O
是否有更好更优雅的方法来实现下面的简单代码(diffYear、A 和 B 是数字): diffYear = yearA - yearB; if (diffYear == 0) { A = B
我正在设计一种语言,并尝试确定 true 应该是 0x01 还是 0xFF。显然,所有非零值都将转换为 true,但我正在尝试确定确切的内部表示。 每种选择的优点和缺点是什么? 最佳答案 没关系,只要
在我的 dialogfragment 类的 OnCreateDialog 中,我正在这样做: AlertDialog.Builder builder = new AlertDialog.Builder
这个问题在这里已经有了答案: Resolving ambiguous overload on function pointer and std::function for a lambda usin
我偶然发现了一个奇怪的 NSDecimalNumber 行为:对于某些值,调用 integerValue、longValue、longLongValue 等,返回意想不到的值(value)。示例: l
这个问题在这里已经有了答案: Resolving ambiguous overload on function pointer and std::function for a lambda using
我有这个正则表达式来测试用户输入是否有效: value.length === 0 || value === '-' || (!isNaN(parseFloat(value)) && /^-?\d+\.
我想用高斯混合模型拟合数据集,数据集包含大约 120k 个样本,每个样本有大约 130 个维度。当我使用 matlab 执行此操作时,我运行脚本(簇号为 1000): gm = fitgmdist(d
我是一名优秀的程序员,十分优秀!