我有一个网站,并且已将其与 glassfish 服务器连接。我想创建可以在此数据库中添加/删除/编辑/搜索数据的按钮。
处理用户代码输入数据的公式:
<form action="./StudentServlet" method="POST">
<table>
<tr>
<td>Sensor ID</td>
<td><input type="text" name="sensorId" value="${sensor.sensorId}" /></td>
</tr>
<tr>
<td>Nazwa</td>
<td><input type="text" name="nazwa" value="${sensor.nazwa}" /></td>
</tr>
<tr>
<td>Opis</td>
<td><input type="text" name="opis" value="${sensor.opis}" /></td>
</tr>
<tr>
<td>Prawa</td>
<td><input type="text" name="prawa" value="${sensor.prawa}" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="action" value="Add">
<input type="submit" name="action" value="Edit">
<input type="submit" name="action" value="Delete">
<input type="submit" name="action" value="Search">
</td>
</tr>
</table>
</form>
<br>
<table border="1">
<th>ID</th>
<th>Nazwa</th>
<th>Opis</th>
<th>Prawa</th>
<c:forEach items="${allSensors}" var="sensor">
<tr>
<td>${sensor.sensorId}</td>
<td>${sensor.nazwa}</td>
<td>${sensor.opis}</td>
<td>${sensor.prawa}</td>
</tr>
</c:forEach>
</table>
用于处理输入数据库的数据的 Servlet:
@WebServlet(name = "StudentServlet", urlPatterns = {"/StudentServlet"})
public class StudentServlet extends HttpServlet {
@EJB
private StudentDaoLocal studentDao;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
String sensorIdStr = request.getParameter("studentId");
int sensorId=0;
if(sensorIdStr!=null && !sensorIdStr.equals(""))
sensorId=Integer.parseInt(sensorIdStr);
String nazwa = request.getParameter("nazwa");
String opis = request.getParameter("opis");
String prawaStr = request.getParameter("prawa");
int prawa=0;
if(prawaStr!=null && !prawaStr.equals(""))
prawa=Integer.parseInt(prawaStr);
Sensor sensor = new Sensor(sensorId, nazwa, opis, prawa);
if("Add".equalsIgnoreCase(action)){
studentDao.addSensor(sensor);
}else if ("Edit".equalsIgnoreCase(action)){
studentDao.editSensor(sensor);
}else if ("Delete".equalsIgnoreCase(action)){
studentDao.deleteSensor(sensorId);
}else if ("Search".equalsIgnoreCase(action)){
sensor = studentDao.getSensor(sensorId);
}
request.setAttribute("sensor", sensor);
request.setAttribute("allSensor", studentDao.getAllSensors());
request.getRequestDispatcher("index.jsp").forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
错误日志:
WARNING: StandardWrapperValve[StudentServlet]: PWC1406: Servlet.service() for servlet StudentServlet threw exception
java.lang.NullPointerException
at com.student.controller.StudentServlet.processRequest(StudentServlet.java:37)
at com.student.controller.StudentServlet.doPost(StudentServlet.java:60)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
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)
我看不到数据。