|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
Java的B/s开发是通常是javaweb开发,又叫J2EE开发,J2SE是手机开发。C#的C/s和B/s开发是说.net和Asp开发。。u在这里说明一点;资深一点的Java和C#程序员都明白一点js|编程|对象|技能|内置对象1.1application
*在jsp中取得application对象
如:getServletContext().setAttribute("counter",newmycount.Counter());
如:<jsp:useBeanscope="application"id="counter"class="mycounter.Counter"/>
*在jsp中处置OnApplicationStart和OnSessionStart事务的办法
利用HttpSessionBindingListener类.
增加session:
session.putValue("bingdings.listener",newMyListener(getServletContext());
界说MyListener类:
importjavax.servlet.http.*;
importjavax.servlet.*;
publicclassMyListenerimplementsHttpSessionBindingListener{
ServletContextcontext;
publicMyListener(ServletContextcontext){
this.context=context;
}
publicvoidvalueBound(HttpSessionBindingEventevent){
System.out.println("valuebound:someonejustboundmylistenertoasession!");
}
publicvoidvalueUnbound(HttpSessionBindingEventevent){
System.out.println("valueunbound:someonejustunboundmylistener!");
}
}
1.2request
*猎取一个正在运转时的jsp/servlet文件的相对url地点
Stringffile=request.getRequestURL();
if(requet.getQueryString()!=null{
file+=?+request.getqueryString();
}
URLreconstructedURL=newURL(request.getScheme(),request.getServerName(),request.getServerPort(),file);
out.println(reconstructedURL.toString());
*猎取客户端经由过程哪个url会见本页面
StringcallPage=request.getHeader("Referer");
*猎取以后剧本在当疥文件体系中的实在路径
request.getRealPath(request.getServletPath());
*判别多个submit中的一个
<inputtype=submitname="sub"value="up">
<inputtype=submitname="sub"value="down">
在jsp中利用request.getParameter("sub");便可分辩
1.3response
*网页重定向之三办法
(1)response.sendRedirect(url);
(2)<%response.setStatus(HttpServletResponse.SC_MOVED_PREMANENTLY);
Stringnowloc="/newpath/index.htm";
response.setHeader("Location",newloc);%>
(3)<jsp:forwardpage="/newpage.jsp"/>
注重上法只能在任何输入还没有发送到客户端之前利用这类办法
*禁用缓存
<%response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires",0);%>
1.4session
*存活工夫
<%session.setMaxInactiveInterval(300);%>
*刊出
session.invalidate();
1.5exception
*在jsp页面中处置Servlet的毛病
protectedvoidsendErrorRedirect(HttpServletRequestrequest,
HttpServletResponseresponse,StringerrorPageURL,Throwablee)
throwsServletException,IOException{
request.setAttibute("javax.servlet.jsp.jspException",e);
getServletConfig().getServletContext();
getRequestDispatcher(errorPageURL).forward(request,response);
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse){
try{
//
}
catch(Exceptione){try{
sendErrorRedirect(request,response,"/jsp/ErrPage.jsp",e);
}catch(Exceptione){e.printStackTrace();}
}
}
*在jsp页面中输入毛病的stacktrace
(1)
<%@pageisErrorPage="true%>
<%
out.println("<pre>");
printWriterpw=response.getWriter();
exception.printStackTrace(pw);
out.println("</pre>");
%>
(2)
<%@pageisErrorPage="true%>
<pre>
<%
exception.printStackTrace(newPrintWriter(out));
%>
</pre>
1.6Cookie
*设置cookie
<%
Cookiemycookie=newCookie("aName","aValue");
response.addCookie(mycookie);
//mycookie.setMaxAge(time);
%>
。
专门做了这个例子;而java的这个例子好像就是为了教学而写的,很多教学目的的例子是不考虑优化、性能的。 |
|