【问题标题】:retrieve the data to the same html page i am using将数据检索到我正在使用的同一 html 页面
【发布时间】:2014-02-23 17:04:46
【问题描述】:

我使用 servlet 代码将数据插入数据库。 我想将该数据检索到同一个 html 页面.....我想要 servlet 代码 在同一类中进行插入和编辑。我正在使用 eclipse .....所以请帮助我 frnds .....我是 Java 新手,并且 我的知识几乎一无所有

html 页面

<!DOCTYPE html>
<html>
<head>

<title>PATIENT</title>
<style type="text/css">
input, textarea,select {
  background-color : lightgray;
}
table
{
border: 2px solid gray;
}
</style>
</head>
<body bgcolor="#DDDDDD">

<script>
function myFunction()
{

    var reg_no=prompt("Please enter your Register No", "")
    }
</script>

<p align ="center"><b>PATIENT</b></p><br>
<form method="post" name ="patientForm" action="patientDemo"> 
<table align="center" cellpadding="2" cellspacing="2">
<tr>
<td>
<font color="maroon">Register Number</font>
</td>
<td colspan="1">
<input type="text" maxlength="15" name="regs_numb" required>
</td>
<td>

   <font color="maroon">PatientId</font>
</td>
<td colspan="2">
    <input type="text" maxlength="10" size="18" name="pat_Id" required>
</td>
<td>
   <font color="maroon">Patient Type</font>
</td>
<td colspan="2">
   <input type="text" size="3" maxlength="3" name="pat_Type">
</td>
<tr>
<td>
   <font color="maroon">Patient Name</font>
</td>
<td colspan="4">
    <input type="text" maxlength="50" size="53" name="pat_Name">
</td>

<td>
<font color="maroon">Age</font>
</td>

<td>
<input type="text" maxlength="3" size="3" name="age">
<select name="age_units">
<option value="years">Years</option>
<option value="months">Months</option>
<option value="days">Days</option>
</select>
</td>
</tr>

<tr>
<td>
  <font color="maroon">Sex</font>
</td>
<td>
<select name="sex">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</td>   



<td>
  <font color="maroon">Package</font>
</td>
<td>
<input type="text" maxlength="10" SIZE="18" name="package">
</td>
</table>


<br>
<table align="center" cellpadding="2">
<tr>
<td>
<input type="submit" value="save" id="save"/>
<td>
<input type="button" onclick="myFunction()" value="edit" id="edit"/>
</td>
<td>
<input type="button" value="delete" id="delete">
</td>
<td>
<input type="reset" value="cancel" id="cancel"/>
</td>
<td>
<input type="button" value="exit" id="exit"/>
</td>
</tr>
</table>
</form>
</body>
</html>

PatientDemo.java

package patientDemo;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




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


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

response.setContentType("text/html");

PrintWriter out= response.getWriter();


    String a=request.getParameter("regs_numb");
    String b=request.getParameter("pat_Id");
    String c=request.getParameter("pat_Type");
    String d=request.getParameter("pat_Name");
    String e=request.getParameter("age");
    String f=request.getParameter("age_units");
    String g=request.getParameter("sex");
    String h=request.getParameter("package");

   try

{
Class.forName("com.mysql.jdbc.Driver");

System.out.println("driver loaded");
System.out.println("Driver is loaded");
Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/charms","root","root");
System.out.println("Connection created");
PreparedStatement ps= ((java.sql.Connection) con).prepareStatement("insert into patient(p_reg_no_v,p_patient_id_v,p_patient_type_v,p_patientname_v,p_age_s,p_ageunit_v,p_sex_v,p_package_v) values (?,?,?,?,?,?,?,?)");


        ps.setString(1,a);
        ps.setString(2,b);
        ps.setString(3,c);
        ps.setString(4,d);
        ps.setString(5,e);
        ps.setString(6,f);
        ps.setString(7,g);
        ps.setString(8,h);
ps.execute();

out.close();
System.out.println("Inserted");

}
catch(Exception e1)

{

System.out.println(e1);

}

}
}

【问题讨论】:

  • 强烈建议遵循 MVC 模式进行实时应用。对于您的问题,您可以使用 AJAX 调用后端并在同一页面中显示结果而无需刷新/重新加载。
  • 先生,我不知道如何使用框架,所以我问你在同一个班............
  • @Saideep 拆分你的任务并用谷歌搜索,你会得到很多好东西。
  • @saideep,我没有提到任何框架。我告诉要遵循 MVC 模式。
  • @saideep 请参阅MVC Example 阅读并理解并尝试去做。如果您发现任何问题,我们随时准备提供帮助

标签: java jsp servlets jdbc


【解决方案1】:

将所有按钮命名为“动作”。在 doPost() 的顶部,通过调用 request.getParameter("action") 来检查按下了哪个按钮,就像您对其他字段所做的那样。

根据值对逻辑进行分支。对于“保存”,你做你已经在做的事情。对于“删除”,您将改为删除 SQL。如果没有设置,什么也不做。

毕竟,让代码查询数据库并呈现页面。

(编辑添加示例)

在 HTML 中,给你的按钮起这样的名字

<input type="submit" value="save" id="save" name="action"/>

还可以像这样为您的数据制作占位符

<input type="text" maxlength="15" name="regs_numb" required value="${regs_numb}">

在java中做我上面描述的

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String action = request.getParameter("action");
    if (action.equals("save")) {
        saveData(request);
    } else if (action.equals("delete")) {
        deleteData(request);
    }
    renderPage(request, response);
}
private void renderPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // load your html file from disk
    String html = loadHtml(filename);

    // load the last data you were just editing
    String a=request.getParameter("regs_numb") // or whatever you primary key field id
    Map<String,String> model = loadSqlData(a);

    // inject the data into your html
    for(Entry<String,String> entry : model.entrySet()) {
        html = html.replace("${" + entry.getKey() +"}", entry.getValue());
    }

    // send the page to the browser
    response.getWriter().println(html);
    response.flush();
}

您需要为 saveData、deleteData、loadHtml 和 loadSqlData 实现这些方法。

对于 loadSqlData 让它返回一个 HashMap,其中键是您放置占位符的所有字段,例如 ${regs_numb),值来自数据库。

如果这看起来工作量很大,那就是。这就是为什么每个人都建议使用可以为您完成 90% 的框架的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 2015-08-20
    相关资源
    最近更新 更多