简单jsp servlet 跳转

jsp、servlet、web.xml


jsp

<%
final String queryString = request.getQueryString();
final String url = request.getContextPath() + "/forwardServlet" + (queryString != null ? "?queryString=" + queryString : "");
response.sendRedirect(response.encodeURL(url));
%>


servlet

package com.msxf.sso.web.servlet;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.msxf.sso.authentication.UserDaoJdbc;

public class ForwardServlet extends HttpServlet {
private static final long serialVersionUID = -3739784153645575408L;

private  UserDaoJdbc userDaoJdbc; 
private File file = null;  
String outfile="";
/**
* 初始化 注入类
*/
public void init(ServletConfig config) throws ServletException {
ServletContext sc =  config.getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
userDaoJdbc = ctx.getBean(UserDaoJdbc.class);
super.init(config);
}


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException   {
String getProject=request.getContextPath().replace("/", "\\");
String ptName=userDaoJdbc.getPtName();
String ptName2=userDaoJdbc.getPtName2();
String ptIconUrl=userDaoJdbc.getIconUrl();
System.out.println(ptIconUrl);
System.out.println(getProject+"\\"+ptIconUrl);
Object ptIconBLob=userDaoJdbc.getIconBlob();
if(ptIconUrl!=""){
this.initIcon(request, ptIconBLob, ptIconUrl);//在指定的路径下,读取Blob生成图片(sso工程与pf目前是分离的, 有必要从库中把图片初始化生成到sso工程中)
}
request.setAttribute("ptName",ptName);
request.setAttribute("ptName2",ptName2);
request.setAttribute("ptIconUrl",getProject+"\\"+ptIconUrl);
request.getRequestDispatcher("login").forward(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}

/**
* 通过数据库Blob字段生成图片
* @param request
* @param photo
* @param iconUrl
* @throws IOException 
*/
public void initIcon(HttpServletRequest request,Object photo,String iconUrl) throws IOException{
outfile=request.getSession().getServletContext().getRealPath("/")+iconUrl;
System.out.println(outfile);
FileOutputStream fos = null;  
try{
byte[] Buffer = new byte[4096];  
InputStream is = new ByteArrayInputStream((byte[]) photo); 
int size = 0; 
file = new File(outfile);  
if(!file.exists()){  
file.createNewFile();     //如果文件不存在,则创建  

fos = new FileOutputStream(file);  
while((size = is.read(Buffer)) != -1){  
fos.write(Buffer,0,size);  
}  
fos.write(Buffer,0,1);  
}catch(Exception e){
System.out.println("[OutPutFile error : ]" + e.getMessage());
}finally{
//关闭用到的资源  
fos.close();  

}
/**
* 读取资源文件信息
* @param filePath
* @throws IOException
*/
public String getProperties(String filePath,String strkey) throws IOException {
Properties pps = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
String strValue = pps.getProperty(strkey);
return strValue;
}
}

web.xml

 <servlet>
    <servlet-name>ForwardServlet</servlet-name>
   <servlet-class>com.msxf.sso.web.servlet.ForwardServlet</servlet-class>
  </servlet>
<servlet-mapping>
   <servlet-name>ForwardServlet</servlet-name>
   <url-pattern>/forwardServlet</url-pattern>
</servlet-mapping>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值