gpt4 book ai didi

java - Servlet 中的 getParameter

转载 作者:行者123 更新时间:2023-12-02 04:44:31 24 4
gpt4 key购买 nike

我正在尝试调用getParameter。一切正常,但是当我单击编辑链接时,它给出了 NullPointerException 。为什么取不到值?

JSP CODE:

  <%@page import="teacher.TeacherBAL"%>
<%@page import="teacher.TeacherBean" %>
<%@page import="java.util.*" %>
<%@ 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>Display Data</title>
</head>
<body>
<table border=1>
<th>Name</th>
<th>Degree</th>
<th>Delete</th>
<th>Edit</th>
<%
ArrayList teachers = (ArrayList) request.getAttribute("allRecord");
if (teachers != null) {
int a = 0;
for (; a < teachers.size(); a++) {
TeacherBean teacherBea = (TeacherBean) teachers.get(a);
%>
<tr>
<td><%=teacherBea.getName()%></td>
<td><%=teacherBea.getDegree()%></td>
<td>
<a href="FetchData?delete=<%=a%>">Delete</a>
</td>
<td>
<a href="FetchData?click=<%=a%>">Edit</a>
</td>
</tr>
<%}
}
%>
</table>
<a href="index.jsp" ><img src="images/back.jpg" height="60"></a>
</body>

Servlet Code:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
response.setContentType("text/html;charset=UTF-8");
String edit= request.getParameter("click");
String del = request.getParameter("delete");
String req= request.getParameter("show");
if(del!=null){
int dele = Integer.parseInt(del);
TeacherBAL.getTeacher().remove(dele);
request.setAttribute("allRecord", TeacherBAL.getTeacher());
RequestDispatcher rd= request.getRequestDispatcher("display.jsp");
rd.forward(request, response);
}
else if(req.equals("ok")){
request.setAttribute("allRecord", TeacherBAL.getTeacher());
RequestDispatcher rd= request.getRequestDispatcher("display.jsp");
rd.forward(request, response);
}
else if(edit!=null){
int num = Integer.parseInt(edit);
PrintWriter out = response.getWriter();
out.print(num);
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name= request.getParameter("names");
String degre= request.getParameter("degre");
if(name!=null && degre!=null){
TeacherBAL.setTeacher(new TeacherBean(name,degre));
RequestDispatcher rd= request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}
}

最佳答案

您收到空指针异常,因为您在单击编辑按钮时没有传递 deleteshow 的值。因此,delreq 为 null,并且您在 req.equals("ok") 上遇到异常。

关于java - Servlet 中的 getParameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29779376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com