<%--
Document : index
Created on : 2018-4-8, 9:30:32
Author : yanzz
--%>
<%@page import="java.util.List"%>
<%@page import="com.huat.model.Student"%>
<%@page import="com.huat.util.DaoFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
首页面
display all student's information
sno
sname
ssex
sage
sdepart
<%
List students = DaoFactory.getStudentDao().getAllStudent();
for (Student s : students) {
%>
<%= s.getSno()%>
<%=s.getSname()%>
<%=s.getSsex()%>
<%=s.getSage()%>
<%=s.getSdept()%>
<%
}
%>
插入一个学生
运行界面
说明:这里直接调用dao方法获取所有学生的名单,缺点层与层之间耦合的过紧,不利于优化。
2.1display01.jsp
<%--
Document : display01
Created on : 2018-4-8, 10:22:08
Author : yanzz
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
JSP Page
display all student's information
sno
sname
ssex
sage
sdepart
${s.getSno()}
${s.getSname()}
${s.getSsex()}
${s.getSage()}
${s.getSdept()}
2.2servlet部分
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.huat.servlet;
import com.huat.model.Student;
import com.huat.util.DaoFactory;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author yanzz
*/
public class DisplayStudent extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
List students = DaoFactory.getStudentDao().getAllStudent();
request.setAttribute("students", students);
request.getRequestDispatcher("display01.jsp").forward(request, response); //内容转发
};
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}//
}
运行结果
3.1insert.jsp部分
<%--
Document : insert
Created on : 2018-4-8, 10:36:52
Author : yanzz
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
插入学生记录
插入学生记录
3.2servlet部分
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.huat.servlet;
import com.huat.model.Student;
import com.huat.util.DaoFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author yanzz
*/
public class InsertStudent extends HttpServlet {
private void print(HttpServletResponse response, Student stu) {
String sno = stu.getSno();
String sname = stu.getSname();
String ssex = stu.getSsex();
int sage = stu.getSage();
String sdept = stu.getSdept();
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException ex) {
Logger.getLogger(InsertStudent.class.getName()).log(Level.SEVERE, null, ex);
}
try {
out.println("");
out.println("");
out.println("");
out.println("Servlet Insert ");
out.println("");
out.println("");
out.println("插入的学生信息内容
");
out.println("学生学号:" + sno);
out.println("学生姓名:" + sname);
out.println("学生性别:" + ssex);
out.println("学生年龄:" + sage);
out.println("学生系部:" + sdept);
out.println("
");
out.println(" ");
out.println("回主页面");
out.println("");
out.println("");
} finally {
out.close();
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, Exception {
response.setContentType("text/html;charset=UTF-8");
String sno = request.getParameter("sno");
String sname = request.getParameter("sname");
String ssex = request.getParameter("ssex");
String sage = request.getParameter("sage");
String sdept = request.getParameter("sdept");
Student stu = new Student();
stu.setSno(sno);
stu.setSname(sname);
stu.setSsex(ssex);
stu.setSage(Integer.parseInt(sno));
stu.setSdept(sdept);
if (DaoFactory.getStudentDao().insertStudent(stu)) {
response.sendRedirect(request.getContextPath() + "/DisplayStudent");
} else {
request.setAttribute("insertErr", "插入信息失败,请检查原因!");
request.getRequestDispatcher("insert.jsp").forward(request, response);
}
}
//
/**
* Handles the HTTP GET
method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (Exception ex) {
Logger.getLogger(InsertStudent.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP POST
method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (Exception ex) {
Logger.getLogger(InsertStudent.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}//
}
3.2.insertStudent的servlet部分
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.huat.servlet;
import com.huat.model.Student;
import com.huat.util.DaoFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author yanzz
*/
public class InsertStudent extends HttpServlet {
/**
* Processes requests for both HTTP GET
and POST
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
private void print(HttpServletResponse response, Student stu) {
String sno = stu.getSno();
String sname = stu.getSname();
String ssex = stu.getSsex();
int sage = stu.getSage();
String sdept = stu.getSdept();
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException ex) {
Logger.getLogger(InsertStudent.class.getName()).log(Level.SEVERE, null, ex);
}
try {
out.println("");
out.println("");
out.println("");
out.println("Servlet Insert ");
out.println("");
out.println("");
out.println("插入的学生信息内容
");
out.println("学生学号:" + sno);
out.println("学生姓名:" + sname);
out.println("学生性别:" + ssex);
out.println("学生年龄:" + sage);
out.println("学生系部:" + sdept);
out.println("
");
out.println(" ");
out.println("回主页面");
out.println("");
out.println("");
} finally {
out.close();
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, Exception {
response.setContentType("text/html;charset=UTF-8");
String sno = request.getParameter("sno");
String sname = request.getParameter("sname");
String ssex = request.getParameter("ssex");
String sage = request.getParameter("sage");
String sdept = request.getParameter("sdept");
Student stu = new Student();
stu.setSno(sno);
stu.setSname(sname);
stu.setSsex(ssex);
stu.setSage(Integer.parseInt(sno));
stu.setSdept(sdept);
if (DaoFactory.getStudentDao().insertStudent(stu)) {
response.sendRedirect(request.getContextPath() + "/DisplayStudent");//地址重定向
} else {
request.setAttribute("insertErr", "插入信息失败,请检查原因!");
request.getRequestDispatcher("insert.jsp").forward(request, response);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (Exception ex) {
Logger.getLogger(InsertStudent.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP POST
method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (Exception ex) {
Logger.getLogger(InsertStudent.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}//
}
运行结果
补充:
关于web一些基本知识
1)http响应状态码用于表示服务器对请求的各种不同处理结果和状态,它是一个三位的十进制数。响应状态
码可归为 5 种类别,使用最高位为 1 到 5 来进行分类,如下所示:
1xx:表示成功接收请求,要求客户端继续提交下一次请求才能完成整个处理过程
2xx:表示成功接收请求并已完成整个处理过程
3xx:重定向--要完成请求必须进行更进一步的操作
4xx:客户端错误--请求有语法错误或请求无法实现
5xx:服务器端错误--服务器未能实现合法的请求
常见状态代码、状态描述、说明:
200 OK //客户端请求成功
400 Bad Request //客户端请求有语法错误,不能被服务器所理解
401 Unauthorized //请求未经授权,这个状态代码必须和 WWW-Authenticate 报头域一起使用
403 Forbidden //服务器收到请求,但是拒绝提供服务
404 Not Found //请求资源不存在,eg:输入了错误的 URL
500 Internal Server Error /服务器发生不可预期的错误
503 Server Unavailable //服务器当前不能处理客户端的请求,一段时间后可能恢复正常
2)get与post的区别
当使用 GET 方式提交表单内容时,浏览器将各个表单字段元素及其数据按照 URL 参数的格式附加在
请求行中的资源路径后面。使用 GET 方式传送的数据量是有限制的,一般限制在 1KB 以下。
如果将网页上的