servlet如何从jsp中获取参数,并把参数在对应的数据库中找到相应的信息,用另一个jsp显示出来
ShowDelete.jsp 要输入信息
<%@ page language="java" import="java.util.*" pageEncoding="gb2312" %>
<html>
<head>
<title>删除学生信息页面</title>
</head>
<body>
<form action="servlet/Delete" method="post">
删除的Id:<input type="text" name="id"/>
<input type="submit" value="确定">
</form>
</body>
</html>
Delete.java :servlet中要接收ShowDelete.jsp的参数,并调用我已经编好的DAO,从数据库中找到相应的信息,再传到另一个Delete.jsp中显示对应行的信息。以下是Delete.java里的doPost函数,不知道对不对。。。
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException,
IOException {
response.setContentType("text/html;charset=gb2312");
//PrintWriter out = response.getWriter();
String id3=request.getParameter("id");
int id = Integer.parseInt(id3);
StudentDAO studentDAO=new StudentDAOImpl();//是个接口
try {
studentDAO.delete(id);调用里边的删除方法
} catch (Exception e) {
System.out.println("不正确");
e.printStackTrace();
}
request.setAttribute("id", id);
ServletContext application=this.getServletContext();
RequestDispatcher rd=application.getRequestDispatcher("/Delete.jsp");
rd.forward(request, response);
}
Delete.jsp中接收参数并显示该怎样写?
------解决方案--------------------servlet里面的写法是没错,
request.getRequestDispatcher("/Delete.jsp").forward(request, response);直接这样写就可以了
获取参数1楼的要用<%%>括起来
也可以用EL表达式获取
------解决方案--------------------Delete.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312" %>
<html>
<head>
<title></title>
</head>
<body>
删除的id为:<%
String id = (String)request.getAttribute("id");
%>
</body>
</html>
或者
Delete.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312" %>
<html>
<head>
<title></title>
</head>
<body>
删除的id为:
${id}
</body>
</html>