最近在做一个项目,要在判断用户是否是通过手机访问网站的,如果是手机访问网站跳到一个登录页面,如果是用户用电脑访问网站跳到另外一个登录页面。我写了以下代码,但是用手机访问网站的时候,跳转不到制定页面,请求回答。
代码如下
servlet代码如下:
package com.lensoon.login;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import com.lensoon.common.JSF;
public class checkLoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Logger logger;
logger = Logger.getLogger(this.getClass());
String userAgent = "";
String HTTP_USER_AGENT = request.getHeader("HTTP_USER_AGENT");
System.out.println("aa"+HTTP_USER_AGENT);
String agent = request.getHeader("User-Agent") == null ? "": request.getHeader("User-Agent");//取用户操作系统信息
//System.out.println(agent);
if(agent != null &&choose_jsp(agent)==true){
logger.error("跳转到手机百度");
logger.info("输出浏览器信息:**********************"+userAgent);
response.sendRedirect("http://www.baidu.com");
}
else{
logger.error("跳转到电脑");
request.getRequestDispatcher("Login.faces").forward(request, response);
}
}
public static boolean choose_jsp(String userAgent) {
if (userAgent.indexOf("Noki") > -1 || // Nokia phones and emulators
userAgent.indexOf("Eric") > -1 || // Ericsson WAP phones and emulators
userAgent.indexOf("WapI") > -1 || // Ericsson WapIDE 2.0
userAgent.indexOf("MC21") > -1 || // Ericsson MC218
userAgent.indexOf("AUR") > -1 || // Ericsson R320
userAgent.indexOf("R380") > -1 || // Ericsson R380
userAgent.indexOf("UP.B") > -1 || // UP.Browser
userAgent.indexOf("WinW") > -1 || // WinWAP browser
userAgent.indexOf("UPG1") > -1 || // UP.SDK 4.0
userAgent.indexOf("upsi") > -1 || //another kind of UP.Browser
userAgent.indexOf("QWAP") > -1 || // unknown QWAPPER browser
userAgent.indexOf("Jigs") > -1 || // unknown JigSaw browser
userAgent.indexOf("Java") > -1 || // unknown Java based browser
userAgent.indexOf("Alca") > -1 || // unknown Alcatel-BE3 browser (UP based)
userAgent.indexOf("MITS") > -1 || // unknown Mitsubishi browser
userAgent.indexOf("MOT-") > -1 || // unknown browser (UP based)
userAgent.indexOf("My S") > -1 ||// unknown Ericsson devkit browser
userAgent.indexOf("WAPJ") > -1 ||//Virtual WAPJAG www.wapjag.de
userAgent.indexOf("fetc") > -1 ||//fetchpage.cgi Perl script from www.wapcab.de
userAgent.indexOf("ALAV") > -1 || //yet another unknown UP based browser
userAgent.indexOf("Wapa") > -1 || //another unknown browser (Web based "Wapalyzer")
userAgent.indexOf("UCWEB") > -1 || //another unknown browser (Web based "Wapalyzer")
userAgent.indexOf("BlackBerry") > -1 || //another unknown browser (Web based "Wapalyzer")
userAgent.indexOf("J2ME") > -1 || //another unknown browser (Web based "Wapalyzer")
userAgent.indexOf("Oper") > -1||
userAgent.indexOf("Android") >-1)
{
return true;
} else {
return false;
}
}
}
web.xml
checkLoginServlet
com.lensoon.login.checkLoginServlet
checkLoginServlet
/checkLoginServlet
输入网址时访问网站的时候会调用这个Login类的构造方法跳转到Login.java这个类中然后用JSF.redirect直接跳转到servlet中,代码如下:
public class Login {
public Login() {
HttpSession session=JSF.getSession();
if(session.getAttribute("success")==null){
try {
session.setAttribute("success", "pc");
JSF.redirect("checkLoginServlet");//直接跳转到servlet中,是可以跳转的
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
代码就是这样,遇到的问题是,用手机访问的时候跳转不到制定页面,等待解答,急用呀!!!!!!!各位大神,来看看把!!!!!!!!!!!
2011年12月23日 20:26