设计思想:1.用1个页面,实现课程录入,提交后直接返回课程界面。2.应用html表单属性进行数据的提交。3.用servlet进行写入数据库和验证输入。
源代码:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘reg.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
.label{
width: 20%
}
.controler{
width: 80%
}
</style>
<script type="text/javascript" src="js/Calendar3.js"></script>
</head>
<body>
<h1>用户注册</h1>
<hr>
<form name="regForm" action="servlet/RegServlet" method="post" >
<p>
<label for="name">姓名</label>
<input type="text" name="name" id="name" />
</p>
<p>年龄
<input type="text" name="age" id="age" />
</p>
<p>反馈
<textarea name="fankui" id="fankui"></textarea>
</p>
<p>
<input type="submit" name="tijiao" id="tijiao" value="提交" />
</p>
</form>
</body>
</html>
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DB.DBUtil;
import Dao.Daoadd;
public class RegServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public RegServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
DBUtil db=new DBUtil();
Connection conn =db.getConn();
String name =request.getParameter("name");
String age =request.getParameter("age");
String fankui =request.getParameter("fankui");
Daoadd add=new Daoadd();
if(!age.equals("王建民")&&!age.equals("刘力嘉")&&!age.equals("刘丹")&&!age.equals("王辉")&&!age.equals("杨子光"))
{
PrintWriter out = response.getWriter();
out.println("");
request.getRequestDispatcher("../fluse.jsp").forward(request,response);
}
else
{
if(!fankui.equals("一教")&&!fankui.equals("二教")&&!fankui.equals("三教")&&!fankui.equals("基教"))
{
request.getRequestDispatcher("../fu.jsp").forward(request,response);
}
else
{
try {
add.add(conn, name, age, fankui);
//response.setHeader("refresh","1;url = index.jsp");
request.getRequestDispatcher("../reg.jsp").forward(request,response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}