【发布时间】:2023-03-18 06:43:01
【问题描述】:
我有一个 jsp 文件 discussion_new.jsp,我试图通过该文件将参数传递给名为 DiscussionServlet 的 servlet,但值没有被传递,请帮忙,我附上以下代码。
这里是discussion_new.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1 align="center"> Start a New Discussion</h1>
<div id="right-container">
<form action="DiscussionServlet" method="post">
<br>
<h1 align="left">Discussion Title</h1>
<textarea name="post-title" id="post-title" rows ="1" cols="90" height="50"
width="200">
</textarea>
<h1 align="left">Discussion Content</h1>
<textarea name="post-detail" id="post-detail" rows="18" cols="90" height="50"
width="200">
</textarea>
<input id="button-submit" type="submit" value="Post" onclick="return confirm('Do
you really want to make this post?');" name="submit"/>
</form>
</div>
</body>
</html>
这里是Servlet 代码:
@WebServlet(name = "DiscussionServlet", urlPatterns = {"/DiscussionServlet"})
public class DiscussionServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession s=request.getSession();
String u_id= (String) s.getAttribute("u_id");
String post_title=request.getParameter("post-title");
String post_detail=request.getParameter("post-detail");
PreparedStatement p1=null;
try {
Connection c1=ConnectionClass.getConnected();
p1=c1.prepareStatement("insert into
discuss(id,disc_title,disc_content)values(?,?,?)");
p1.setString(1, u_id);
p1.setString(2, post_title);
p1.setString(3, post_detail);
}
catch(Exception e)
{
}
finally {
out.close();
}
}
@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";
}// </editor-fold>
}
sql语法和属性值没有错误,我已经检查过了,我也从代码中删除了额外的cmets。希望有人帮忙
【问题讨论】: