切换到手机端的时候出啊先500
来源:3-1 项目应用-多端应用切换
cj啦啦啦啦
2020-01-04 12:40:07
package com.imooc.servlet;
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;
import com.imooc.device.Device;
import com.imooc.device.DeviceFactory;
@WebServlet("/index.html")
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Device device=DeviceFactory.getDevice(request);
request.getRequestDispatcher(device.getIndex()).forward(request, response);
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>这是手机端界面</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>这是pc端界面</h1>
</body>
</html>
package com.imooc.device;
public class DesktopDevice implements Device{
public String getIndex(){
return "/desktop/index.html";
}
public String getDescription(){
return "/desktop/desc.html";
}
}
package com.imooc.device;
public interface Device {
public String getIndex();
public String getDescription();
}
package com.imooc.device;
public class MobileDevice implements Device{
public String getIndex(){
return "/mobile/index.html";
}
public String getDescription(){
return "/mobile/desc.html";
}
}
package com.imooc.device;
import javax.servlet.http.HttpServletRequest;
public class DeviceFactory {
public static Device getDevice(HttpServletRequest request){
String userAgent=request.getHeader("user-agent");
System.out.println(userAgent);
if(userAgent.indexOf("Windows NT")!=-1){
return new DesktopDevice();
}
else if(userAgent.indexOf("iphone")!=-1){
return new MobileDevice();
}
else{
return null;
}
}
2回答
kevin唯心
2020-01-17
=、= 我又搞错了 逻辑有点混乱
好帮手慕小班
2020-01-04
同学你好,1、切换到手机端时报出的500异常的具体报错信息是什么,是空指针异常吗,建议将具体的报错信息贴出。
2、查看同学贴出代码,在判断是否为手机端(iPhone)时,建议同学注意单词的大小写,将iphone修改给iPhone,再来试一下。
查看请求头中的手机端标志:
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题
回答 1
回答 3