读书人

JAVA工作代码小札记

发布时间: 2012-09-21 15:47:26 作者: rapoo

JAVA工作代码小笔记

1、input表单回车事件?

onkeypress="if(event.keyCode==13) checksubmit();"

?

2、弹出页面

? ? 1)window.open("view.html","_blank");

?

? ? 2)//窗口属性

? ? ? ? ? var openStatus = "dialogWidth=830px;dialogHeight=450px;scroll:no;status:no;help:no";

? ? ? ? ?//弹出对话框

? ? ? ? ? var retVal = window.showModalDialog("view.html", "", openStatus);

3、页面传递给后台值出现乱码

? ? 前台JSP页面:

? ? ? var name = "张三";

? ? ? var param = "?name="+encodeURI(encodeURI(name));?

? ? 后台代码

? ? ?String name = request.getParameter("name");

? ? ?name =?URLDecoder.decode(name, "utf-8");

?

4、项目中文件路径

? // 项目的更路径,在linux、windows系统中都可以使用

? ?String path = request.getSession().getServletContext().getRealPath("/");

? //path-->> ? D:\apache-tomcat-6.0.35\webapps\sdemo\

? ?String upload = path + File.separator + "upload" + File.separator + "test.doc";

------------------------------------------------

?//以下只能在windows系统中有用,在linux没有用,但不需要request

?

String sysPath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

//sysPath-->> ? /D:/apache-tomcat-6.0.35/webapps/sdemo/WEB-INF/classes/yangcj/frame/interceptor/MyInterceptor.class

String upload = sysPath.substring(1, sysPath.indexOf("WEB-INF")) + "upload" +?File.separator??+ "test.doc";? (觉得这个方法非常不好,太繁琐了)

?

搜集得到:

?1)JSP中获得当前应用的相对路径和绝对路径
? 根目录所对应的绝对路径:request.getRequestURI()
? 文件的绝对路径  :application.getRealPath(request.getRequestURI());
? 当前web应用的绝对路径 :application.getRealPath("/");
? 取得请求文件的上层目录:new File(application.getRealPath(request.getRequestURI())).getParent()

2) Servlet中获得当前应用的相对路径和绝对路径
? 根目录所对应的绝对路径:request.getServletPath();
? 文件的绝对路径 :request.getSession().getServletContext().getRealPath(request.getRequestURI()) ?
? 当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath("/");
? (ServletContext对象获得几种方式:
? javax.servlet.http.HttpSession.getServletContext()
? javax.servlet.jsp.PageContext.getServletContext()
? javax.servlet.ServletConfig.getServletContext()
? )
3)Java类中获得绝对路径
 根据java.io.File的Doc文挡,可知: 默认情况下new File("/")代表的目录为:System.getProperty("user.dir")。

4)获得项目名称

??????? request.getContextPath()

?

5、JSP页面与页面传递值

? ? 在父级页面中打开第一个页面

?

? ?//窗口属性

? ? var openStatus = "dialogWidth=830px;dialogHeight=600px;scroll:no;status:no;help:no";

? ? //弹出对话框

? ? var retVal = window.showModalDialog(url, "",openStatus);

?

子级页面设置返回值

?

? ?var array = new Array();

? ? array[0] = "1";

? ? array[1] = "2";

? ? window.returnValue = array;

? ? window.close();

?

父级页面值

? ?var value1 = retVal[0];

? ?var value2 = retVal[1];

?

6、在Web项目中关于流的使用之后要关闭

? ? InputStream/OutputStream、PrintWriter、POI/JXL的workbook 、Socket

?

读书人网 >编程

热点推荐