利用servlet对客户端增删改的请求进行响应

今天简单学习了servlet,它是java服务器端的程序组件,可以对客户端的请求进行响应。

一、我们若想用它,需要创建一个类,继承HttpServlet。一般的我们都要重写它的doGet方法和doPost方法来响应客户端的提交。

一般都是一个表对应一个servlet,我依旧已教师为例,发一下我做的,代码如下:

 

package com.jsp.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdbc.entity.TeacherEntity;
import com.jdbc.manager.SchoolManager;

public class Teacher_Servlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String caozuo=request.getParameter("caozuo");
		if(caozuo.equals("add")){
			this.addNewTeacherInfo(request, response);
		}
		if(caozuo.equals("del")){
			this.delTeacherInfoById(request, response);
		}
		if(caozuo.equals("upd")){
			this.updTeacherInfo(request, response);
		}
	}
	private void addNewTeacherInfo(HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		String tid=request.getParameter("tid");
		String tname=request.getParameter("tname");
		String tage=request.getParameter("tage");
		TeacherEntity teacher=new TeacherEntity();
		teacher.setTid(new BigDecimal(tid));
		teacher.setTname(tname);
		teacher.setTage(new BigDecimal(tage));

		boolean f=SchoolManager.addNewTeacherInfo(teacher);
		if(f){
			response.sendRedirect("InforShow/teacherManager.jsp");
		}else{
			response.sendRedirect("InforManager/addTeacher.jsp?add=0");
		}
		
	}
	private void delTeacherInfoById(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String tid=request.getParameter("tid");
		boolean f=SchoolManager.delTeacherInfoById(tid);
		if(f){
			System.out.println("OK");
		response.sendRedirect("InforShow/teacherManager.jsp");
		}
	}
	private void updTeacherInfo(HttpServletRequest request, HttpServletResponse response) throws IOException{
	
		String tid=request.getParameter("tid");
		String tname=request.getParameter("tname");
		String tage=request.getParameter("tage");
		TeacherEntity teacher=new TeacherEntity();
		teacher.setTid(new BigDecimal(tid));
		teacher.setTname(tname);
		teacher.setTage(new BigDecimal(tage));
		
		boolean f=SchoolManager.updTeacherInfo(teacher);
		if(f){
			response.sendRedirect("InforShow/teacherManager.jsp");
		}
	}
}


 

二、类写完之后,我们必须要在web.xml中进行配置,详细配置信息如下:

  
   tservlet//名字自定义起,但必须要和下面的中name一致。
   com.jsp.servlet.Teacher_Servlet//servlet类的路径。这里是供服务器所用
 

 
  tservlet
  /teacherservlet//这里时供客户端所用。其中/表示的就是在webRoot根目录下后面是我们在调的时候用的地址。
 

 

三、页面中的请求(添加教师信息),注意的是action的位置teacherservlet,提交方式为post。为了给servlet指定执行何种方法,我们加了一个隐藏域

,告诉servlet这个请求执行的是添加方法,详细代码如下:

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'addTeacher.jsp' starting page
    
	
	
	    
	
	
	
	

	
	

  
  <%
  String add=request.getParameter("add");
  if(add!=null){%>
   
  <%} %>
  
   
添加教师信息

编        号:

姓        名:

年        龄:

         


 

修改和删除也是一样的,只要我们给servlet传入一个操作指令,它就会根据你给定的value进行对应的操作。利用servlet我们不用对每一个请求都建一个.jsp文件进行操作。而是一个表对应一个,对其进行响应。今天刚学,有不足的地方希望大家多多指教我,谢谢。

你可能感兴趣的:(学习心得,Java)