gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:43:21 28 4
gpt4 key购买 nike

我的 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>`

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

}

一切正常,我得到结果,但不在同一页面我想在同一页面显示Servlet的结果是否可以?如果不是我必须做什么才能在同一页面中显示结果

最佳答案

我建议您将 html 代码与 servlet 分开。维护起来很困难。

关于java - 我想在同一个 HTML 页面中显示 servlet 的结果。这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29329777/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com