日期:2014-05-17 浏览次数:20728 次
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <form name="form1" action="addMessage.jsp" method="get"> <table> <tr> <td>姓名:</td> <td><input type="text" name="name"/></td> </tr> <tr> <td>邮箱:</td> <td><input type="text" name="email"/></td> </tr> <tr> <td>主题:</td> <td><input type="text" name="title"/></td> </tr> <tr> <td>留言:</td> <td><textarea name="content"></textarea></td> </tr> <tr> <td><input type="submit" value="提交留言"></td> <td><input type="reset" value="重新填写"></td> </tr> </table> </form> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="model1.*" %> <jsp:useBean id="message" scope="page" class="model1.Message" > <jsp:setProperty name="message" property="*"/> </jsp:useBean> <jsp:useBean id="messageBean" scope="page" class="model1.MessageBean" /> <html> <body> <% messageBean.setMessage(message); messageBean.addMessage(); out.println("success"); %> </body> </html>
package model1; public class Message { private String name; private String email; private String title; private String content; private java.sql.Date date; public void setName(String name) { this.name=name; } public void setEmail(String email) { this.name=email; } public void setTitle(String title) { this.name=title; } public void setContent(String content) { this.name=content; } public String getName() { return this.name; } public String getEmail() { return this.email; } public String getTitle() { return this.title; } public String getContent() { return this.content; } public void setDate(java.sql.Date date) { this.date=date; } public java.sql.Date getDate() { return this.date; } }
package model1; import java.sql.*; public class MessageBean { private Connection con; Message msg; public MessageBean() { String CLASSFORNAME="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String SERVANDDB="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=myjsp"; String USER="sa"; String PWD="123456"; try{ Class.forName(CLASSFORNAME); con=DriverManager.getConnection(SERVANDDB,USER,PWD); } catch(Exception e){ e.printStackTrace(); } } public void setMessage(Message msg){ this.msg=msg; } public void addMessage()throws