简单jsp servlet 跳转

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wanggeying/article/details/52807510

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>

猜你喜欢

转载自blog.csdn.net/wanggeying/article/details/52807510
今日推荐