简体   繁体   English

我想在同一HTML页面中显示servlet的结果,这可能吗?

[英]I want to display the results from servlet in the same HTML page.Is that possible?

My HTML file is follows,The name of Servlet was Results 我的HTML文件如下,Servlet的名称是Results

 <form action="Results" method="post">
    <table border="1">
    <tr><th colspan="2">STUDENT Results</th></tr>
    <tr><td>Name:-</td><td><input type="text" name="name"></td></tr>
    <tr><td>Hallticket:-</td><td><input type="text" name="hallticket"></td></tr>
    <tr><td><input type="submit" name="submit" value="submit"></td><td><input type="reset" value="Reset"></td></tr>
    </table>
    </form>`

My Servlet Code is as follows 我的Servlet代码如下

public class Results extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String ht=request.getParameter("hallticket");
        //String p=request.getParameter("userPass");
        //String e=request.getParameter("userPhone");
        //String c=request.getParameter("userCountry");

        try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/result","root","");
        /*PreparedStatement ps=con.prepareStatement("insert into exp values(null,?,?,?)");*/
        PrintWriter out1=response.getWriter();
        PreparedStatement pst = con.prepareStatement("select * from data where hallticket=?");
        pst.setString(1, ht);
        ResultSet res = pst.executeQuery();

        if(res.next())
        {
            String name = res.getString(2);
            String hlt= res.getString(3);
            Integer m1 = res.getInt(4);
            Integer m2 = res.getInt(5);
            Integer m3 = res.getInt(6);
            Integer total = res.getInt(7);
            Float average=res.getFloat(8);
            String grade= res.getString(9);
            String result= res.getString(10);           
            //Display values
            out1.println("<table border='1'>");
            out1.println("<tr><td>");
            out1.println("<h3>Name:-</td><td>" + name +"</h3></td></tr>");
            out1.println("<tr><td><h3>Hallticket:-</td><td>" + hlt +"</h3></td></tr>");
            out1.println("<tr><td><h3>m1:-</td><td>" + m1 +"</h3></td></tr>");
            out1.println("<tr><td><h3>m2:-</td><td>" + m2 +"</h3></td></tr>");
            out1.println("<tr><td><h3>m3:-</td><td>" + m3 +"</h3></td></tr>");
            out1.println("<tr><td><h3>Total:-</td><td>" + total +"</h3></td></tr>");
            out1.println("<tr><td><h3>Percentage:-</td><td>" + average +"</h3></td></tr>");
            out1.println("<tr><td><h3>Grade:-</td><td>" + grade +"</h3></td></tr>");
            out1.println("<tr><td><h3>Results:-</td><td>" + result +"</h3></td></tr>");
            out1.println("<h6>Back To <a href='results.html'>Home</a> </h6>");
             /*System.out.print("ID: " + name);
             System.out.print("Password: " + pw);
             System.out.print("Phone: " + ph);*/


             /*ps.setString(1,n);
            ps.setString(2,p);
            ps.setString(3,e);
            //ps.setString(4,c);
            */

        }else{
            out1.println("No user with Hallticket: "+ht+ " Exist");
            out1.println("<h6>Back To <a href='results.html'>Results</a> </h6>");
        }
        pst.close();
        }
        catch (Exception e2) {System.out.println(e2);}

        out.close();
    }

}

Everything was working Iam getting results,but not in same page I want to display the results of Servlet in Same page Is it possible? 一切正常我正在获取结果,但不在同一页面中我想在同一页面中显示Servlet的结果,这可能吗? If not what I have to do to display results in same page 如果不是,我必须要做的是在同一页面中显示结果

i suggest you that please separate html code from servlet. 我建议您将html代码与servlet分开。 it is hard to maintain. 很难维护。

try ajax 尝试ajax

HTML: HTML:

<form>
    <table border="1">
        <tr><th colspan="2">STUDENT Results</th></tr>
        <tr><td>Name:-</td><td><input type="text" id="name"></td></tr>
        <tr><td>Hallticket:-</td><td><input type="text" id="hallticket"></td></tr>
        <tr>
            <td><input type="button" value="submit" onclick="postToResults()"></td>
            <td><input type="reset" value="Reset"></td>
        </tr>
    </table>
</form>`
<div id="results"></div>

JavaScript: JavaScript的:

<script type="text/javascript">
    function postToResults() {
        var params = {'name' : document.getElementById("name").value, 'hallticket' : document.getElementById("hallticket").value};
        $.ajax({
            type: 'POST',
            url: 'Results',
            data: params,
            async: false,
            success: function (result) {
                document.getElementById('results').innerHTML=result;
            }
        });
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM