很奇怪呀。运行成功后,再登录又会跳到登录页面
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class MainServlet extends HttpServlet {
//创建一个线程池
private ExecutorService service=Executors.newFixedThreadPool(10);
private void setCookie(/*String server,*/String ticket) {
service.submit(new Runnable() {
@Override
public void run() {
HttpPost httpPost=new HttpPost("http://localhost:8081/webApp1"+"/setCookie?ticket="+ticket);
CloseableHttpClient httpClient=null;
CloseableHttpResponse response=null;
try {
httpClient=HttpClients.createDefault();
response=httpClient.execute(httpPost);
HttpEntity entity=response.getEntity();
String responseContent =EntityUtils.toString(entity, "utf-8");
/*SysteSm.out.println(responseContent);*/
}catch(Exception e){
e.printStackTrace();
} finally {
if(null!=response) {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=httpClient) {
try {
httpClient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//如果请求类型的ServletPath是/main则运行下面代码
if(request.getServletPath().equals("/main")) {
//获取请求中发送过来的domains即其他app的url
String domains =request.getParameter("domains");
/* System.out.println(domains);*/
//获取请求中的tickct
String ticket=request.getParameter("ticket");
//把获得的domains按,分割成一个个app
for(String server:domains.split(",")
) {
//调用setCookie方法把cookie给每个app都添加上
/*System.out.println("server="+server);*/
if(server!=null&&!server.trim().equals("")) {
setCookie(ticket);
}
}
//转发到main.jsp,显示主要内容
request.getRequestDispatcher("main.jsp").forward(request, response);
//处理其他app以setCookie发过来的内容,存入Cookie
}else if(request.getServletPath().equals("/setCookie")) {
String ticket=request.getParameter("ticket");
System.out.println("ticket2="+ticket);
Cookie cookie=new Cookie("Ticket_Granting_Ticket",ticket);
cookie.setMaxAge(60*60);
cookie.setPath("/webApp2");
response.addCookie(cookie);
response.setCharacterEncoding("utf-8");
response.setContentType("application/text;charset=utf-8");
PrintWriter out=null;
try {
out =response.getWriter();
out.write("ok");
} catch(IOException e) {
e.printStackTrace();
}finally {
if(null!=out) {
out.close();
}
}
}
}
}
正在回答
HttpPost httpPost=new HttpPost("http://localhost:8081/webApp1"+"/setCookie?ticket="+ticket);
先登录的是8081还是8082?如果是8081先登录,然后在8081中调用触发线程让8082登录,那么上面这行代码应该是8082以及webApp2
祝学习愉快!
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星