如何用JSP+SERVLET+JAVABEAN实现数据的添加和修改

niaochun 2006-04-15 08:36:06
我想向一学生表(ID,NAME)中用MVC模式插入和修改信息,不知道SERVLET怎么写,请各位高手指点
...全文
566 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dp_555 2006-04-17
  • 打赏
  • 举报
回复
友情接分。。。
a233106052 2006-04-16
  • 打赏
  • 举报
回复
最上面的是正解,用JAVABEAN封装数据。
JSP显示,SERVLET控制,JAVABEAN封装。这就是MVC~!
二楼的用了struts架构,如果你会用的话那当然更好了~!
niaochun 2006-04-16
  • 打赏
  • 举报
回复
能不能提供个例子程序?
zuoyangguang 2006-04-16
  • 打赏
  • 举报
回复
首先你要知道几个步骤:
一你会不会写servlet,如果不会,先看看相关资料,然后用jbuilder自动生成servlet,运行,看看web.xml的配置
二你会不会在jsp中调用servlet,如果不会,先看看相关资料,然后看看我给你的例子
三你会不会写jdbc,如果不会,先看看相关资料,然后看看我给你的例子
zuoyangguang 2006-04-16
  • 打赏
  • 举报
回复
输入的jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<title>
jsp3
</title>
</head>
<body bgcolor="#ffffff">
<h1>
JBuilder Generated JSP
</h1>
<form method="post" action="servlet">
<br><br>
<input type="text" name="studentid"/>
<input type="text" name="studentname"/>
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>

servlet内容为
package servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class Servlet2
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";

//Initialize global variables
public void init() throws ServletException {
}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
}

public void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String studentid = request.getParameter("studentid");
String studentname = request.getParameter("studentname");
String dbname =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=chat";
String dbuser = "sa";
String dbpassword = "123";

Connection conn = null;
PreparedStatement preparedStmt = null;
String sql = "insert into table values(?,?)";
try {
conn = java.sql.DriverManager.getConnection(dbname, dbuser,
dbpassword);
preparedStmt = conn.prepareStatement(sql);
preparedStmt.setString(1, studentid);
preparedStmt.setString(2, studentname);
preparedStmt.execute();
}
catch (SQLException ex) {
System.out.println("DBModel connect error:SQLException");

}
catch (Exception e) {
System.out.println("DBModel connect error:Exception");

}
finally {
try {
preparedStmt.close();
conn.close();
}
catch (SQLException ex) {

}

}

}

//Clean up resources
public void destroy() {
}
}

只能帮到这儿了,剩下的自己慢慢琢磨吧
riso8371 2006-04-15
  • 打赏
  • 举报
回复
我写的思想是这样的不知对不对
首先要在插入的页面,将数据提交给Servlet,在Servlet中调用JavaBean,然后从JavaBean返回好数据到Servlet,再转发回你要转发的页面或是一个结果页(显示是否成功类似的东东)
转发用这个方法
request.setAttribute("属性名",属性值);
RequestDispatcher dispatcher=request.getRequestDispatcher("转发的JSP页面");
dispatcher.forward(request,response);

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧