ITPub博客

首页 > IT基础架构 > 网络安全 > Now the first servlet about C.R.U.D

Now the first servlet about C.R.U.D

原创 网络安全 作者:nonamedemo 时间:2007-08-28 22:37:42 0 删除 编辑

Connecting with DataBase//

import javax.servlet.*;//导包
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.xaccp.vo.Student;
//服务层
public class AddStudentServ 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 {
response.setContentType(CONTENT_TYPE);//反映类型也设置成标准码
String userName = request.getParameter("txtName");//获取对象通过request
String sex1[] = request.getParameterValues("sex");//用于获取客户端一组同名的表单元素
int sex[] = new int[sex1.length];//sex是单选框所以结果集返回一个参数
for (int i = 0; i < sex.length; i++) {//装入整形数组
sex[i]=Integer.parseInt(sex1[i]);
}
int age = Integer.parseInt(request.getParameter("txtAge"));//年纪的获取,强转一下

Student stu = new Student();//new一个新对象
stu.setStuName(userName);//把对象的名字和获取到的名字一挂钩
stu.setAge(age);//同上
stu.setSex(sex[0]);//同上,但只取第一个


}

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

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

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10395457/viewspace-966588/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: chat room
请登录后发表评论 登录
全部评论
  • 博文量
    31
  • 访问量
    16671