• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

JSP问答集 (给初接触jsp者)

servlet/jsp 搞代码 7年前 (2018-06-18) 163次浏览 已收录 0个评论

如何混合使用jsp和SSI #include?
jsp中可以使用如下方式包含html
<!–#include file="data.inc"–>
但是如果data.inc中包含JSP CODE ,我可以使用:
<%@include file="data.inc"%>
如何绦幸程安全的JSP?

http://www.gaodaima.com/42489.htmlJSP问答集 (给初接触jsp者)

只需增加如下指令<br>
<%@ page isThreadSafe="false" %>
JSP如何HTML FORM中的Y料?
通^戎玫request物件即可,如下:
<%<br>
String item = request.getParameter("item");
int howMany = new Integer(request.getParameter("units")).intvalue();
%>
在JSP如何包含一oB文件?
oB包含如下:<%@ include file="copyright.html" %>
B包含如下:<jsp:include page="copyright.html" flush="true"/>
在JSP中如何使用注?
主要有四中方法:
1。<%– c –%>
2。//
3。/**c**/
4。<!–c–>
在JSP中如何绦g[重定向?
使用如下方式即可:response.sendRedirect("http://ybwen.home.chinaren.com/index.html&quot;;);
也能物理地改HTTP HEADER傩裕缦拢
<%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn="/newpath/index.html";
response.setHeader("Location",newLocn);
%>
如何防止在JSP或SERVLET中的出不被BROWSER保存在CACHE中?
把如下_本加入到JSP文件的_始即可:
<%
response.setHeader("Cache-Control","no-store"); file://HTTP 1.1
response.setHeader("Pragma","no-cache"); file://HTTP 1.0
response.setDateHeader ("Expires", 0); file://prevents caching at the proxy server
%>
在JSP中如何O置COOKIE?
COOKIE是作?HTTP HEADER的一部分被l送的,如下方法即可O置:
<%
Cookie mycookie = new Cookie("aName","avalue");
response.addCookie(mycookie);
%>
在JSP中如何h除一COOKIE?
<%
Cookie killMyCookie = new Cookie("mycookie", null);
killMyCookie.setMaxAge(0);
killMyCookie.setPath("/");
response.addCookie(killMyCookie);
%>
在一JSP的求碇腥绾瓮VJSP的绦?
如下例:
<%
if (request.getParameter("wen") != null) {
// do something
} else {
return;
}
%>
在JSP中如何定x方法?
你可以定x方法,但是你不能直接LJSP的戎梦锛峭ㄟ^档姆椒鬟f。如下:
<%!
public String howBadFrom(HttpServletRequest req) {
HttpSession ses = req.getSession();

return req.getRemoteHost();
}
%>
<%
out.print("in general,lao lee is not baddie ");
%>
<%= howBadFrom(request) %>

如果BROWSER已P]了COOKIES,在JSP中我如何打_SESSION砀?
使用URL重即可,如下:
hello1.jsp
<%@ page session="true" %>
<%
Integer num = new Integer(100);
session.putvalue("num",num);
String url =response.encodeURL("hello2.jsp");
%>
hello2.jsp</a>
hello2.jsp
<%@ page session="true" %>
<%
Integer i= (Integer )session.getvalue("num");
out.println("Num value in session is "+i.intvalue());
%>

在JSP中能l送EMAIL?
可以使用SUN的S冒sun.net.smtp包。如下_本使用SmtpClientl送EMAIL。
<%@ page import="sun.net.smtp.SmtpClient, java.io.*" %>
<%
String from="[email protected]";
String to="[email protected], [email protected]";
try{
SmtpClient client = new SmtpClient("mail.xxxxx.xxx");
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("To: " + to);
message.println("Subject: Sending email from JSP!");
message.println("This was sent from a JSP page!");
message.println();
message.println("Cool! :-)");
message.println();
message.println("Good Boy");
message.println("I’m in genius.com");
message.println();
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e);
}
%>

在SERVLET中我能{用一JSPe`?
然]},如下展示了如何在一SERVLET控制卧日{用一JSPe`面。
protected void sendErrorRedirect(HttpServletRequest request,
HttpServletResponse response, String errorPageURL,
Throwable e)
throws ServletException, IOException {
request.setAttribute ("javax.servlet.jsp.jspException", e);
getServletConfig().getServletContext().
getRequestDispatcher(errorPageURL).forward(request,
response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) {
try {
// do something
} catch (Exception ex) {
try {
sendErrorRedirect(request,response,"/jsp/MyErrorPage.jsp",ex);
} catch (Exception e) {
e.printStackTrace();
}
}
}
JSP如何cEJB SessionBean通?
下面的代a段作了很好的示
<%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject,
foo.AccountHome, foo.Account" %>
<%!
file://定x一SessionBeanHome介面例的全局引用
AccountHome accHome=null;
public void jspInit() {
file://@得Home介面例
InitialContext cntxt = new InitialContext( );
Object ref= cntxt.lookup("java:comp/env/ejb/AccountEJB");
accHome = (AccountHome)PortableRemoteObject.narrow(ref,AccountHome.class);
}
%>
<%
file://例化SessionBean
Account acct = accHome.create();
file://{用h端方法
acct.doWhatever(…);
// 如此等等
%>

欢迎大家阅读《JSP问答集 (给初接触jsp者)》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:JSP问答集 (给初接触jsp者)
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址