后台java代码取 request ,response ,application , session 怎么取?
后台java代码取 request ,response ,application , session 怎么取?
比如
public class HttpSessionBinding implements HttpSessionBindingListener {
...}
HttpSessionBinding 类里
问题点数:20、回复次数:5Top
1 楼doway(john)回复于 2006-03-03 23:02:22 得分 20
操作参数有 getSession 方法,通过 session 的 getServletContext 方法又可以获得 applicaton。
request 和 response 好像就无法获得了。
Top
2 楼netwar(huangls518@gmail.com)回复于 2006-03-03 23:07:27 得分 0
用HttpServlet继承吧
package cn.com.huangls.enterprise;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class system extends HttpServlet
{
private String sysConfigFile="config";
private HttpServletRequest request=null;
private HttpServletResponse response=null;
public void init(HttpServletRequest req,HttpServletResponse res) throws Exception{
this.request=req;
this.response=res;
}
public String getParaStr(String obj,int len,String defaultValue){
String tempValue="";
if(request==null) return "";
tempValue=request.getParameter(obj);
if(tempValue==null){
tempValue="";
}
tempValue=tempValue.trim();
if(tempValue.equals("")){
tempValue=defaultValue;
}
return tempValue;
}
public Connection getConnection() throws Exception{
Connection conn=null;
String driver=getProperties("jdbc.driver");
String server=getProperties("jdbc.server");
String user=getProperties("jdbc.user");
String password=getProperties("jdbc.password");
try{
Class.forName(driver);
}catch(Exception ex){
throw new Exception(getProperties("error","NoFindDriver"));
}
try{
conn=DriverManager.getConnection(server,user,password);
}catch(Exception ex){
throw new Exception(getProperties("error","ConnectError")+ex.toString());
}
return conn;
}
public ArrayList executeQuery(String strSQL) throws Exception{
ResultSet res1=null;
Statement comm=null;
Connection tempConn=null;
ArrayList arrayList=new ArrayList();
try{
tempConn=getConnection();
if(tempConn!=null){
comm=tempConn.createStatement();
res1=comm.executeQuery(strSQL);
ResultSetMetaData myStr=res1.getMetaData();
while(res1.next()){
Hashtable b=new Hashtable();
for(int i=1;i<=myStr.getColumnCount();i++){
String name=myStr.getColumnName(i);
String value=res1.getString(i);
System.out.println(name);
System.out.println(value);
b.put(name,value);
}
arrayList.add(b);
}
}
}catch(Exception ex){
throw new Exception(ex.toString());
}finally{
res1.close();
comm.close();
tempConn.close();
}
return arrayList;
}
public String getProperties(String field){
return getProperties(sysConfigFile,field);
}
public String getProperties(String file,String field){
String tempValue="";
try{
ResourceBundle bundle=ResourceBundle.getBundle(file);
tempValue=bundle.getString(field);
}catch(Exception ex){
System.out.println("Error:"+ex.toString());
}
return tempValue;
}
//转换成中文
public String toGBK(String tempValue){
String defaultValue=tempValue;
try{
defaultValue=new String(defaultValue.getBytes("iso8859-1"),"GBK");
}catch(Exception ex){
System.out.println(ex.toString());
}
return defaultValue;
}
}
Top
3 楼xuefeng1986(薛丰)回复于 2006-03-04 00:35:23 得分 0
想要问什么?是获得对象还是取内容?
取内容是这么取:
在servlet或者JAVA文件中可以取到前台JSP的控件中的内容 request.getParameter("控件名")
在JSP中用session.setAttribute("name","value")
在JAVA中用session.getAttribute("name")
不知道这么回答对不对?
Top
4 楼greki(锐╃→)回复于 2006-03-04 13:54:01 得分 0
谢谢,大家
通过 session 的 getServletContext 方法又可以获得 applicaton。
可能,request 和 response 是获得不了.
session超时时我想不一定有request 和 response .
对这么方面理解不深...望高人指点.
狗年发财.:)
Top
5 楼xiongbing528(多情剑客无情剑)回复于 2006-03-04 20:36:16 得分 0
scope ..scopeTop




