Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 试图在同一页中显示输入的值_Java_Jsp_Servlets_Jdbc - Fatal编程技术网

Java 试图在同一页中显示输入的值

Java 试图在同一页中显示输入的值,java,jsp,servlets,jdbc,Java,Jsp,Servlets,Jdbc,我试图在同一页中显示输入的值, 当用户在字段中输入值并提交时,这些值应该保存数据库并显示在同一个jsp页面中。 我尝试了,这些值存储在数据库中,但不显示在同一页上,但它显示空值,然后在执行提交操作时div消失 我在数据库中使用存储过程 这是jsp页面: <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE htm

我试图在同一页中显示输入的值, 当用户在字段中输入值并提交时,这些值应该保存数据库并显示在同一个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>

您尝试通过使用RequestDispatcher转发请求和响应时,也可以使用DOM on body load display:none。加载后不会显示,因为整个页面在此处刷新

删除函数bodyLoad(),而不是此函数,在div上写入内嵌样式。例如:-

<div id="display" style="display:none;">//your dom goes here </div>
//您的dom位于此处

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

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

    }
}

是的,我做了,但它显示空值,然后当提交操作启动时div消失performed@vicky您正在onload事件中调用
bodyload
函数。您应该检查bodyload函数本身和call show()函数中的请求参数是否不为null。它工作得很好,请尝试让我们分享经验。这不是一个好的设计模式。请尝试学习并实现MVC设计模式。嗨,sumesh TG.,我想为提问者提供解决方案。感谢您提供有关设计实施的信息。
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();
        }

    }
}