其实我现在觉得也挺蒙的,主要是对于页面跳转的相对路径和绝对路径的把握经常糊涂,所以先记下来一点点到时候再找到更好的方法的时候再作补充。

其实现在很多都是用框架进行开发了,但是框架技术与jsp技术还是有很大的关系的,以前倒是经常没认真细看,所以还是作一下笔记好自己能慢慢体会。因为对这些技术自己以前都米有深入了解。。。所以记下来一些简单点的笔记~~~~

application:(ServeltContext实例)

该实例代表jsp所属的web应用本身,可用于jsp页面或在servlet之间交换信息,常用方法setAttribute(name,value),getAttribute(name),getInitParameter(name)

request:(HttpServeltContext实例)

对象封装了一次请求,客户端的请求参数都被封装到该对象里。常用方法getParameter(name),getParameterValues(name),setAttribute(name,value),getAttribute(name),setCharacterEncoding(encode)

1.利用application获取web应用配置参数

/kernal/getparam.jsp

<body>
   <%
   //从配置参数中获取驱动、url、数据库的用户名和密码
    String driver = application.getInitParameter("driver");
    String url = application.getInitParameter("url");
    String user = application.getInitParameter("user");
    String pass= application.getInitParameter("pass");
    //注册驱动
    Class.forName(driver);
    //获得数据库;连接
    Connection conn = DriverManager.getConnection(url,user,pass);
    //创建statement对象
    Statement st = conn.createStatement();
    ResultSet rs = st.executeQuery("select * from user");
   %>
   <table>
    <%   while(rs.next()){
    %>
        <tr>
            <td><%=rs.getString(1) %></td>
            <td><%=rs.getString(2) %></td>
            <td><%=rs.getString(3) %></td>
        </tr>
    <%} %>
   </table>
  </body>

application的getInitParameter(name)方法获取web应用配置参数,而这些参数是保存到web.xml中的context-param元素中的,所以下一步可以看一下web.xml的配置

<context-param>
   <param-name>driver</param-name>
   <param-value>com.mysql.jdbc.Driver</param-value>
 </context-param>

这里的param-name指明名字是driver,param-value指明驱动的名字。按这样的格式可以继续设置url,user,pass,名字其实都是任意定义的,只要保证getparam.jsp上面的名字和web.xml的名字一一对应起来就可以了

按照getparam.jsp的参数来看,会显示user表中每一行的前3列。


然后下面是关于留言板的例子,日志开篇的application和request联系起来

/msg/msg_ed.jsp

这个页面是用于准备添加留言的页面

先大概写一下主要框架:

/*num为留言数,一开始进入jsp时,num=0,后面再通过application的getAttribute("num")方法取得当前留言簿中的num数
*/
<%
String temp = new String();
temp = (String)application.getAttribute("num");
if(temp == null)
    application.setAttribute("num", "0");
%>
//然后table中有3个参数
//标题:<name="txtti"> 作者:<name="txtau"> 内容:<name="txtau">
//其中前两者是tetx类型,最后是textarea类型
<type="submit" value="添加添加">
<type="reset" value="重置">
<type="button" value="查看所有留言">

详细的代码:

<html>
  <head>   
    <title>My JSP 'msg_ed.jsp' starting page</title>   
   <script type="text/javascript">
   function MM_openWindow(theURL,winName,features){
       window.open(theURL,winName,features);
   }
   </script>
  </head>
                                                                                                                                                                                                                                                                                              
  <body>
    <%
    String temp = new String();
    temp = (String)application.getAttribute("num");
    if(temp == null)
        application.setAttribute("num", "0");
    %>
    <h3>留言板</h3>
    <hr size="1">
    <form action="msg_ok.jsp" method="post" name="form1">
        <table align ="center" width="60%">
            <tr><td width="20%">留言作者:</td>
            <td width="80%"><input type="text" name="txtau"/></td>
            </tr>
            <tr><td width="20%">留言标题:</td>
            <td width="80%"><input type="text" name="txtti"/></td>
            </tr>
            <tr><td width="20%">留言内容:</td>
            <td width="80%"><textarea name="txtnr" cols="45" rows="5"></textarea></td>
            </tr>
            <tr>
            <td><input type="submit" name="btadd" value="添加到留言簿"/></td>
            <td><input type="reset" name="btres" value="重新填写留言"/></td>
            <td><input type="button" name="btrea" value="查看所有留言"
             onClick="MM_openWindow('msg_sh.jsp'),'','width=400,height=400'"/></td>
            </tr>
        </table>
    </form>
  </body>
</html>


/msg/msg_ok.jsp

添加留言成功的页面

<body>
    <h3>留言板</h3>
    <hr size="1">
    <%
    int n;
    String temp = new String();
    String temp1 = new String();
    String temp2 = new String();
    String temp3 = new String();
    temp1 = request.getParameter("txtau");
    temp2 = request.getParameter("txtti");
    temp3 = request.getParameter("txtnr");
    n = temp1.length()*temp2.length()*temp3.length();
    if(n!=0){
        temp = (String)application.getAttribute("num");
        n = Integer.parseInt(temp);
        n = n+1;
        temp = temp.valueOf(n);
        application.setAttribute("num", temp);
        application.setAttribute("ti"+temp, temp1);
        application.setAttribute("au"+temp, temp2);
        application.setAttribute("nr"+temp, temp3);
    %><p align="center">留言成功
    <%}else{ %>
        <p align="center">不添加标题、作者或内容,留言失败!
    <%} %>
    <p align="center"><a href="msg_ed.jsp">返回首页</a>
  </body>

简单的分析一下:通过request.getParameter(name)的方法获得上一次页面提交的请求的参数(标题、作者、内容等客户端参数)。前面提及到的request对象就是用来封装一次请求的,我们需要通过这个对象来获得客户端请求的参数,这时我们就可以得到txtau,txtti,txtnr的值。事实上,我们的留言数会随着我们的添加而一直上升的,要如何区分对象呢?靠的就是每次新建一次留言就增加的数n。每一条留言都有其对应的n(其实就跟我们平常的id号类似,唯一标识一段留言),这时application对于每一条留言的参数都通过setAttribute(name,value)方法使得ti1和temp1对应起来,ti2和下一个temp1对应起来。


/msg/msg_sh.jsp

<%
int n;
String temp = new String();
String temp1 = new String();
String temp2 = new String();
String temp3 = new String();
temp = (String)application.getAttribute("num");
n = Integer.parseInt(temp);
if(n == 0){
%><p align="center">目前没有文章
<%}else{ %>
    <table width="60%" border="1" bordercolor="#999999">
    <%
    int i;
    for(i=1;i<=n;i++){
        temp = temp.valueOf(i);
        temp1 = (String)application.getAttribute("ti"+temp);
        temp2 = (String)application.getAttribute("au"+temp);
        temp3 = (String)application.getAttribute("nr"+temp);
        %>
        <tr>
        <td bgcolor="#CCCCFF" height="30">
        <b><%=temp%>.标题 :<%=temp1 %> 作者:<%=temp2 %></b>
        </td>
        </tr>
        <tr><td><%=temp3 %></td></tr>
    <%}%>
    </table>   
<%} %>

通过for循环输出所有的留言。

整理一下思路:

msg_ed.jsp的num值通过(application的get或setAttribute方法获得,看是不是第一个),form表单summit后提交到msg_ok.jsp,msg_ok.jsp通过request封装上一次的请求,获得一次留言request.getParameter()方法取得各个参数值,新增一个留言n=n+1,此时num值通过application.set的方法更新值,下一次获得的时候就是已经更新后的num了。另外,为了区分留言,此时的n和一个留言对应起来setAttribute("ti"+temp);最后在msg_sh.jsp中通过for就可以把所有留言输出。


另外,因为一开始测试的时候是放到index.jsp页面,那时通过

<!--<jsp:include page="msg/msg_ed.jsp"></jsp:include>  -->
<!-- <jsp:forward page="msg/msg_ed.jsp"></jsp:forward> -->

这两种方法来进入,

两者对应的URL都为:localhost/jspkernal(其中jspkernal为对应的project名)

那么include和forward有什么区别呢~~查了查~~include是将一个页面包括进去,也就是一个jsp里面通过多个include可以包含多个jsp的,forward是请求转发另一个资源,也就是把资源给覆盖到我们的index.jsp中了~~正因为如此,两者的URL都不会变化。

另外,kernal是放到WebRoot下的,也有人习惯放到WEB-INF下,不过我还在纠结着应该怎样放到WEB-INF下,目前如果放到这个目录下的话我只能通过绝对路径找到jsp页面,很明显,很麻烦的,每次都输很长的字符串。。。不过应该还有别的方法,我继续探索吧。。。

另外,就像我之前是放到index.jsp上面测试的,这样不太好(就这个代码而言),因为程序是从localhost/jspkernal这一路径出发的,所以当向msg_ed.jsp的form action是到"msg_ok.jsp",此时很明显就会跳转到localhost/jspkernal/msg_ok.jsp,但我的msg_ok.jsp的详细目录应该是/jspkernal/msg/msg_ok.jsp,所以我还是得好好再测试测试多几次了解了解相对路径以怎么好的方式才可以很好地在各个页面中跳转额。。。。≡(▔﹏▔)≡