【发布时间】:2019-01-19 18:46:15
【问题描述】:
我试图在同一页面中显示输入的值, 当用户在字段中输入值并提交时,值应保存数据库并应显示在同一 jsp 页面中。 我试过了,值存储在数据库中,但没有显示在同一页面上,但是,它显示空值,然后执行提交操作时 div 消失 我在数据库中使用存储过程
这是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>Stored Procedure</title>
<script>
function show(){
document.stp.submit();
document.getElementById("display").style.display="block";
}
</script>
</head>
<body>
<form name="stp" action="STP" >
ID:<input type="text" name="ID"><br>
Name:<input type="text" name="Name"><br>
Branch:<input type="text" name="Branch"><br>
MobileNo:<input type="tel" name="Mobile"><br>
<input type="button" value="Submit" onclick="show()">
</form>
<div id="display" style="display:none">
Id:<%= request.getAttribute("id") %><br>
Name:<%= request.getAttribute("name") %><br>
Branch:<%= request.getAttribute("branch") %><br>
MobileNo:<%= request.getAttribute("mobile") %><br>
</div>
</body>
</html>
这是 servlet 代码:
package task;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/STP")
public class STP extends HttpServlet {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int ID = Integer.parseInt(request.getParameter("ID"));
System.out.println(ID);
String Name = request.getParameter("Name");
String Branch = request.getParameter("Branch");
long Mobile =Long.valueOf(request.getParameter("Mobile"));
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vikas", "root", "root");
CallableStatement cs = con.prepareCall("{call Procedure3(?,?,?,?)}");
cs.setInt(1, ID);
cs.setString(2, Name);
cs.setString(3,Branch);
cs.setLong(4,Mobile);
cs.execute();
int id=cs.getInt(1);
String name=cs.getString(2);
String branch=cs.getString(3);
long mobile=cs.getLong(4);
request.setAttribute("id",id);
request.setAttribute("name",name);
request.setAttribute("branch",branch);
request.setAttribute("mobile",mobile);
request.getRequestDispatcher("StoredProcedure.jsp").forward(request, response);
System.out.println(id);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
【问题讨论】: