[java]代码库
package com.servlet;
import java.io.IOException;
import java.nio.charset.Charset;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class servletdemo extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
//初始化
public void init() throws ServletException {
System.out.println("我是init()方法!用来进行初始化工作");
}
//处理GET请求
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doPost(request, response);
}
//处理POST请求
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8");
String password = request.getParameter("password");
HttpSession session = request.getSession();
session.setAttribute("username", username);
session.setAttribute("password", password);
response.sendRedirect("/servletTest/success.jsp");
System.out.println("用户名:"+username);
System.out.println("密码:"+password);
}
//销毁实例
public void destroy() {
super.destroy();
System.out.println("我是destroy()方法!用来进行销毁实例的工作");
}//源代码片段来自云代码http://yuncode.net