会话变量在java中重新加载页面时失败

时间:2014-04-18 05:49:39

标签: java html mysql jsp

如果重新加载(view_blog_comment)页面后如何永久保存会话变量的值?

我正在研究一个主要项目(使用语言java)...其中我有模块(博客)。博客的工作是......当我们点击主页上的链接时...即。博客... blog.jsp页面开放...然后有3个链接 - 1.创建博客 2.查看其他博客 3.查看自己的博客

当我们点击查看其他博客...在那个(viewOtherBlog.jsp)页面我们可以查看所有其他博客...有标题和内容...我已经将标题作为这样的超链接... 博客标题 - > "><%= rs.getString(2)%>

当我们点击这个标题时,我们跳转到另一个页面..我们得到详细的博客视图。跳转到新页面后,我们获得有关该博客的详细信息...可以选择评论博客和发表评论也可以在下面查看。

所有事情都写到...评论帖子按钮没有被点击... 单击按钮时....页面重新加载..页面重新加载后 - 标题和内容详细信息将丢失。

(view_Blog.jsp)

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import='java.sql.*'%>

<% 

  HttpSession s=request.getSession();
  String uid=(String)s.getAttribute("u_id");



 Class.forName("com.mysql.jdbc.Driver");
 Connection conn=DriverManager.getConnection("jdbc:mysql:
//localhost:3306/major_db","root", "root");
%>


<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>


    <%
  try
    {

    PreparedStatement p1=conn.prepareStatement("select * from blog where pid !=   '"+uid+"' ");
     ResultSet rs=p1.executeQuery();

    %>



              <% while(rs.next()==true)
                           {

               %>
           <table>
                <tr>
            <td>Blog Author --></td>
                   <td>
                   <%=rs.getString(5)%>   
                   </td>
          </tr>                         
          <tr>

           <td>Blog title --></td>
   <td><a href="blog_read_comment.jsp?bid=<%=rs.getInt(1)%>"><%=rs.getString(2) %></td>
        //when above link is clicked the value is stored in bid
        </tr>
        <tr>
            <td>Blog Content --></td>
            <td><%=rs.getString(3)%></td>
        </tr>
        <% 

                   }
 }
 catch(Exception e)
 {
     out.println(e);
 }

 %>
    </table>       
</body>
</html>




    </table>       
    </body>
    </html>

(read_blog_comment.jsp)

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import='java.io.PrintWriter' %>
<%@page import='java.sql.*'%>
<%@page import="java.util.*" %>
<%@page import='javax.servlet.http.HttpSession' %>


<% 
String b_id=request.getParameter("bid");//value of bid is stored in b_id
HttpSession s=request.getSession();//session is created to ]
//permanently save the value of bid
s.setAttribute("b_id",b_id);
String bid=(String)s.getAttribute("b_id");
problem is in above 4 lines....


 //value of bid 
//is lost when page gets reloaded
//i am not able to understand why this happens
//becoz of this problem when i comment on the blog the blog_name,blog_content is lost

Class.forName("com.mysql.jdbc.Driver");
Connection conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/major_db",   "root", "root");
%>


<% 


     try
     {

        PreparedStatement p1=conn.prepareStatement("select * from
        blog where   bid='"+bid+"';");
        ResultSet rs=p1.executeQuery();

%>


<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>

<body>

    <table>
        <% if(rs.next()==true)
                           {
                               %>
        <tr>

            <td><h4>Blog title</h4></td>
        </tr>
        <tr>
            <td><%=rs.getString(2) %></td>
        </tr>
        <tr>
            <td><h4>Blog Content</h4></td>
        </tr>
        <tr>
            <td><%=rs.getString(3)%></td>
        </tr>
        <% 
        }
 }
 catch(Exception e)
 {
     out.println("Exception"+e);
 }

 %>
 </table>


 <form action="blog_Comment_Servlet" method="post">
     <h3> Post a Comment </h3>
     <textarea name="post_comment" required="required" 
     id="post-comment" rows="3"    cols="50">
     </textarea>
     <input  type="submit" id="button-submit" value="Post" name="submit"  />        
 </form>


 <%
    try
    {
        PreparedStatement p1=conn.prepareStatement("select * from 
        blog_comment where bid='"+bid+"';");
        ResultSet rs=p1.executeQuery();
  %>
  <table border="0"> 
      <label>Comments:</label>

       <% 
        while(rs.next())
         {

       %>
        <tr>

            <td><h4>By-</h4></td>

        </tr>
        <tr>
            <td><%=rs.getString(6) %></td>
            <td><%=rs.getString(5) %></td>
        </tr>
        <tr>
            <td><h4>Comment - </h4></td>
        </tr>
        <tr>
            <td><%=rs.getString(4)%></td>
        </tr>


        <%      
    }
    }
    catch(Exception e)
    {
        out.println("Exception - "+e);
    }
     %>
   </table>


   </body>
</html>

(blog_comment_servlet编码)

   <%@page contentType="text/html" pageEncoding="UTF-8"%>
   <%@page import='java.io.PrintWriter' %>
   <%@page import='java.sql.*'%>
   <%@page import="java.util.*" %>
   <%@page import='javax.servlet.http.HttpSession' %>


 <% 
 String b_id=request.getParameter("bid");
 HttpSession s=request.getSession();
 s.setAttribute("b_id",b_id);
 String bid=(String)s.getAttribute("b_id");

 Class.forName("com.mysql.jdbc.Driver");
 Connection conn =DriverManager.getConnection("jdbc:mysql:
 //localhost:3306/major_db",    "root", "root");
 %>


 <% 


     try
     {

        PreparedStatement p1=conn.prepareStatement("select * from 
        blog where bid='"+bid+"';");
        ResultSet rs=p1.executeQuery();

   %>


   <!DOCTYPE html>
   <html>
   <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
   </head>

    <body>

    <table>
        <% if(rs.next()==true)
                           {
                               %>
        <tr>

            <td><h4>Blog title</h4></td>
        </tr>
        <tr>
            <td><%=rs.getString(2) %></td>
        </tr>
        <tr>
            <td><h4>Blog Content</h4></td>
        </tr>
        <tr>
            <td><%=rs.getString(3)%></td>
        </tr>
        <% 
        }
 }
 catch(Exception e)
 {
     out.println("Exception"+e);
 }

 %>
 </table>


 <form action="blog_Comment_Servlet" method="post">
     <h3> Post a Comment </h3>
     <textarea name="post_comment" required="required"
      id="post-comment" rows="3" cols="50">
                </textarea>
         <input  type="submit" id="button-submit" value="Post" name="submit"  />        
 </form>


 <%
    try
    {
        PreparedStatement p1=conn.prepareStatement("select * from 
        blog_comment where bid='"+bid+"';");
        ResultSet rs=p1.executeQuery();
  %>
  <table border="0"> 
      <label>Comments:</label>

       <% 
        while(rs.next())
         {

       %>
        <tr>

            <td><h4>By-</h4></td>

        </tr>
        <tr>
            <td><%=rs.getString(6) %></td>
            <td><%=rs.getString(5) %></td>
        </tr>
        <tr>
            <td><h4>Comment - </h4></td>
        </tr>
        <tr>
            <td><%=rs.getString(4)%></td>
        </tr>


        <%      
    }
    }
    catch(Exception e)
    {
        out.println("Exception - "+e);
    }
%>
  </table>


</body>
</html>

0 个答案:

没有答案