loginindex.jsp:
<%@ page language="java" import="java.util.*,java.net.* " pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
try{
Cookie[] cookies = request.getCookies();
if(cookies != null){
for(int i = 0; i < cookies.length; i++){
if(cookies[i].getName().equals("username")){
String username=URLDecoder.decode(cookies[i].getValue(), "utf-8");
request.setAttribute("username",username);
}
if(cookies[i].getName().equals("password")){
String password= URLDecoder.decode(cookies[i].getValue(), "utf-8");
request.setAttribute("password",password);
}
}
}
}catch(Exception e){
e.printStackTrace();
}
%>
登录注册
LoginIndexServlet:
package info.test.controller;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginIndexServlet extends HttpServlet {
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String msource="http://mobile.51bi.com/login.do";
String username=request.getParameter("username");
String password=request.getParameter("password");
String a1=request.getParameter("a1");
String a2=request.getParameter("a2");
if("uname".equals(a1)){
System.out.println("a1不为空");
try{
Cookie cookie1 = new Cookie("username", username);
cookie1.setMaxAge(30 * 60 * 24 * 30 * 2); //用户名保留1个月
response.addCookie(cookie1);
}catch(Exception ex){
ex.printStackTrace();
}
}else{
System.out.println("a1为空");
}
if("upass".equals(a2)){
System.out.println("a2不为空");
try{
Cookie cookie2 = new Cookie("password", URLEncoder.encode(password,"utf-8"));
cookie2.setMaxAge(30 * 60 * 24 * 30 * 2); //用户名保留1个月
response.addCookie(cookie2);
}catch(Exception ex){
ex.printStackTrace();
}
}else{
System.out.println("a2为空");
}
msource=msource+"?username="+username+"&password="+password;
System.out.println(msource);
URL url = new URL(msource);
InputStream is = url.openStream();
byte[] b = new byte[is.available()];
is.read(b);
String resultString=new String(b, "utf-8");
String tempString=new String(resultString.getBytes("iso-8859-1"),"utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
System.out.println(resultString);
out.print(resultString);
is.close();
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}