尝试在同一页面中显示输入的值

时间:2018-08-13 05:07:54

标签: java jsp servlets jdbc

我试图在同一页面中显示输入的值, 当用户在字段中输入值并提交时,这些值应保存数据库并显示在同一jsp页面中。 我试过了,值存储在数据库中,但没有显示在同一页面上,但是,它显示空值,然后在执行提交操作时div消失 我在数据库中使用存储过程

这是jsp页面:

onDestroy()

这是servlet代码:

<%@ 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>

2 个答案:

答案 0 :(得分:0)

您尝试通过使用RequestDispatcher转发请求和响应,以及在身体负荷显示上使用DOM:无。加载后将不会显示,因为此处刷新了整个页面。

删除函数bodyLoad()代替此函数,在div上编写内联样式。例如:-

<div id="display" style="display:none;">//your dom goes here </div>

答案 1 :(得分:0)

请尝试以下代码,它将在您的系统中完美运行。

JSP代码:-

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="task.STP"%>
<!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 bodyload() {
        document.getElementById("display").style.display = "none";
    }
    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>
    <%
        if (STP.display) {
    %>

    <div id="display">
        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 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 {
    public static boolean display = false;
    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);
            display = true;
            request.getRequestDispatcher("index.jsp").forward(request, response);
            System.out.println(ID);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}