【问题标题】:Trying to display entered values in same page尝试在同一页面中显示输入的值
【发布时间】: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();
        }

    }
}

【问题讨论】:

    标签: java jsp servlets jdbc


    【解决方案1】:

    您尝试使用 RequestDispatcher 转发请求和响应,以及您在 body load display:none 上使用 DOM。加载后不会显示,因为这里刷新了整个页面。

    去掉bodyLoad()函数代替这个,在div上写内联样式。例如:-

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

    【讨论】:

    • 是的,但是,它显示空值,然后在执行提交操作时 div 消失
    • @vicky 您在 onload 事件中调用 bodyload 函数。您应该检查 bodyload 函数本身的请求参数是否为空并调用 show() 函数。
    【解决方案2】:

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

    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();
            }
    
        }
    }
    

    【讨论】:

    • 它运行良好,请尝试让我们分享经验。
    • 这不是一个好的设计模式。请尝试学习并实现 MVC 设计模式。
    • 嗨 sumesh TG.,我正在尝试为提问者提供解决方案。感谢您提供设计实施信息。
    猜你喜欢
    • 2021-06-20
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多