ssm框架實現介面基礎上再加上token判斷

IT_95發表於2018-12-28

HttpServletRequest request,HttpServletResponse response相關知識可詳見:https://blog.csdn.net/IT_95/article/details/85322924

以下為控制層程式碼

package com.zb.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zb.biz.AccesstokenBiz;

@Controller
public class UrlResponse {
@Autowired
private AccesstokenBiz biz;

@ResponseBody
@RequestMapping("/response")
public void urlResponse(HttpServletRequest request,HttpServletResponse response){
	String controller =request.getParameter("controller");
	String token =request.getParameter("token");
	String url="http://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()
			+"/"+controller+"?"+request.getQueryString();
	System.out.println(url);
	int numToken=biz.searchToken(token);
	if (numToken==1) {
		try {
			response.sendRedirect(url);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}else{
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print("Please login again.");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}

}


}

response.sendRedirect(url) //直接跳轉至url 傳送一個臨時相應到客戶端

printWriter out   //輸出字元流

相關文章