码迷,mamicode.com
首页 > 其他好文 > 详细

软件工程概论——课堂测试1

时间:2017-11-28 19:53:11      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:call   mit   用户注册   sub   row   代码   jsp   工程   str   

设计思想: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
}

}

软件工程概论——课堂测试1

标签:call   mit   用户注册   sub   row   代码   jsp   工程   str   

原文地址:http://www.cnblogs.com/aishanyishi/p/7911551.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!