该博客主要关于java servlet编程,开发servlet三种方法
关于目录前三条,都是开发servlet的方法,发展过程按目录先后次序,目前HttpServlet最广泛
有一点要注意,每写一个servlet都要部署一次
开发servlet要引入servlet-api.jar
package cn.linjie;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class hello implements Servlet{
//销毁servlet实例(释放内存),关闭tomcat是调用
@Override
public void destroy() {
System.out.println("destroy");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
//该init函数用于初始化该servlet,该函数只会被调用一次(当用户第一次访问该servlet时被调用)
@Override
public void init(ServletConfig arg0) throws ServletException {
System.out.println("init it");
}
//service函数用于处理业务逻辑,业务逻辑代码都写在这
//当用户访问servlet时,都会调用该函数
//res:用于获得客户端(浏览器)的信息
//ron:用于向客户端(浏览器)返回信息
@Override
public void service(ServletRequest res, ServletResponse ron) throws ServletException, IOException {
System.out.println("service");
//打印在浏览器上
PrintWriter pw=ron.getWriter();
pw.println("hello,this is servlet");
}
}
hello
name
cn.linjie.hello
name
/sp
servlet生命周期有四个阶段
结果
只需要重写service方法
package cn.linjie;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
public class helloGen extends GenericServlet {
@Override
public void service(ServletRequest res, ServletResponse ron) throws ServletException, IOException {
System.out.println("service..");
PrintWriter pw=ron.getWriter();
pw.println("hello,this is a GenericServlet");
}
}
hello
name
cn.linjie.hello
name
/sp
name2
cn.linjie.helloGen
name2
/sp2
需要重写doGet和doPost方法
package cn.linjie;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/helloHttp")
public class helloHttp extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public helloHttp() {
super();
}
/*
*doGet写业务逻辑
*doPost调用doGet
*/
//处理get请求
//res获取浏览器信息
//ron向浏览器返回信息
protected void doGet(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {
PrintWriter pw=ron.getWriter();
pw.println("hello,this is a HttpServlet");
}
//处理post请求
//为什么doGet合可以写在doPost中,因为两个方法代码一样,这样调用可以避免代码重复
protected void doPost(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {
doGet(res, ron);
}
}
hello
name
cn.linjie.hello
name
/sp
name2
cn.linjie.helloGen
name2
/sp2
name3
cn.linjie.helloHttp
name3
/sp3
package cn.linjie;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 登录界面
*/
@WebServlet("/helloHttp")
public class logindemo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public logindemo() {
super();
// TODO Auto-generated constructor stub
}
/*
*doGet写业务逻辑
*doPost调用doGet
*/
//处理get请求
//res获取浏览器信息
//ron向浏览器返回信息
protected void doGet(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {
//处理中文乱码
ron.setContentType("text/html;charset=utf-8");
PrintWriter pw=ron.getWriter();
pw.println("");
pw.println("");
pw.println("登录 ");
pw.println("登录
");
pw.println("");
pw.println("");
pw.println("");
}
//处理post请求
//为什么doGet合可以写在doPost中,因为两个方法代码一样,这样调用可以避免代码重复
protected void doPost(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {
doGet(res, ron);
}
}
package cn.linjie;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 登录验证
*/
@WebServlet("/loginC")
public class loginC extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public loginC() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {
//接受用户名和密码(res是获取浏览器信息)
String u=res.getParameter("username");
String p=res.getParameter("passwd");
//验证
if(u.equals("xlj")&&p.equals("123"))
{
//合法
//跳转
ron.sendRedirect("welcome");
}else {
//不合法
//跳转(参数是跳转至的url)
ron.sendRedirect("login");
}
}
protected void doPost(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {
doGet(res, ron);
}
}
package cn.linjie;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Welcome
*/
@WebServlet("/Welcome")
public class Welcome extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Welcome() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter pw=ron.getWriter();
pw.println("welcome");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(res, ron);
}
}
logindemo
cn.linjie.logindemo
logindemo
/login
loginC
cn.linjie.loginC
loginC
/loginc
Welcome
cn.linjie.Welcome
Welcome
/welcome