我正在尝试从mysql中保存数据并在另一个页面中检索它。我的保存部分成功但无法检索。它正在抛出此错误 严重:带有路径[/ BackUp]的上下文中servlet [jsp]的Servlet.service()引发异常[java.lang.NullPointerException]的根本原因 显示java.lang.NullPointerException
AddEvents.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<h1 align="center">Add Event</h1>
<script>
function myFunction() {
document.getElementById("myTime").value = "22:53:05";
}
function mySubmitFunction() {
alert("You added an Event");
}
</script>
</head>
<body>
<form action="AddEventsServlet" method="post" onsubmit="mySubmitFunction()">
<table align="center" border= "10px" cellpadding="10">
<tr>
<td>
<label>Title :</label><input type="text" name="Title" required>
</td>
</tr>
<tr>
</tr>
<tr>
<td>
<label>Details:</label><textarea rows="4" cols="15" name="EventDesc"></textarea>
</td>
</tr>
<tr>
<td>
Start time:<input type="time" id="myTime" name="StartTime">
</td>
</tr>
<tr>
<td>
End time:<input type="time" id="myTime" name="EndTime">
</td>
</tr>
<tr>
<tr>
<td>
Approximate Budget:<input type="text" id="budget" name="Budget">
</td>
</tr>
<!-- <tr>
<td>
Transportation:<select name="transportation" name="Transportation">
<option value="Bus">Bus</option>
<option value="MRT">MRT</option>
<option value="Cab">Cab</option>
<option value="Walk">Walk</option>
</select>
</td>
</tr>-->
<!-- -<tr>
<td>
Environment: <input type="radio" name="Indoor" value="Indoor" name="Environment"> Indoor
<input type="radio" name="Outdoor" value="Outdoor">Outdoor<br>
</td>
</tr>
<tr>-->
<tr>
<td>
<button type="submit" value="Add">Add</button> <button type="reset" value="Reset">Reset</button>
</td>
</tr>
</table>
<a href="index.jsp">
<img border="0" alt="W3Schools" src="cal.jpg" width="50" height="50">
</a>
</form>
</body>
</html>
AddEvents.java:
package model;
import java.sql.Date;
public class AddEvents {
private String title;
private String eventDesc;
private String StartTime;
private String EndTime;
private String Budget;
//private String Transportation;
//private String Environment;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getEventDesc() {
return eventDesc;
}
public void setEventDesc(String eventDesc) {
this.eventDesc = eventDesc;
}
public String getStartTime() {
return StartTime;
}
public void setStartTime(String startTime) {
StartTime = startTime;
}
public String getEndTime() {
return EndTime;
}
public void setEndTime(String endTime) {
EndTime = endTime;
}
public String getBudget() {
return Budget;
}
public void setBudget(String budget) {
Budget = budget;
}
Retrieve.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@page import="model.AddEvents" %>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Events added</h2>
<!-- Retrieving the session
object AddEvents and saved it
in a variable named myEvent -->
<%!AddEvents myEvent;%>
<%myEvent=(AddEvents)session.getAttribute("iAddEvents"); %>
<table>
<tr><td>Title:</td><td><%= myEvent.getTitle() %></td></tr>
<tr><td>Details:</td><td><%= myEvent.getEventDesc() %></td></tr>
<tr><td>StartTime:</td><td><%=myEvent.getStartTime() %></td></tr>
<tr><td>EndTime:</td><td><%= myEvent.getEndTime() %></td></tr>
<tr><td>Budget:</td><td><%=myEvent.getBudget() %></td></tr>
</table>
</body>
</html>
AddEventsServlet:
package servlet;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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;
import database.DBAO;
import model.AddEvents;
/**
* Servlet implementation class AddEventsServlet
*/
@WebServlet("/AddEventsServlet")
public class AddEventsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddEventsServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
AddEvents myEvent = new AddEvents();
//create an object based on the Java class customers
//Assignment of data
myEvent.setTitle(request.getParameter("Title"));
myEvent.setEventDesc(request.getParameter("EventDesc"));
myEvent.setStartTime(request.getParameter("StartTime"));
myEvent.setEndTime(request.getParameter("EndTime"));
myEvent.setBudget(request.getParameter("Budget"));
//myEvent.setTransportation(request.getParameter("Transportation"));
//myEvent.setEnvironment(request.getParameter("Environment"));
String title = request.getParameter("Title");
String eventDesc= request.getParameter("EventDesc");
//This will convert string to date format
String StartTime=request.getParameter("StartTime");
String EndTime =request.getParameter("EndTime");
String Budget=request.getParameter("Budget");
try
{
DBAO myDatabase = new DBAO();
AddEvents myEventDetails =myDatabase.isEvent(myEvent,title, eventDesc, StartTime, EndTime, Budget);
myDatabase.AddEvents(myEvent,title,eventDesc,StartTime,EndTime,Budget);
if(myEventDetails!=null){
HttpSession mySession = request.getSession(true);
mySession.setAttribute("iAddEvents", myEvent);//setting the object named myEvent into the session
request.getRequestDispatcher("Retrieve.jsp").forward(request,response);
}
else{
request.getRequestDispatcher("AddEvent.jsp").forward(request,response);
}
}
catch(Exception ex)
{
System.out.println("Error Accessing Database:" +ex);
}
}
}