Java Web 第二个问题----session问题

来源:互联网 发布:十大网络融资平台 编辑:程序博客网 时间:2024/05/21 20:29

 我做的项目遇到的session获得不到值得主要原因是:session值得大小写不一致。需要耐心。

经验:以后再有关于session的问题时需要将session的名字复制即可。不要凭空的记。

session的用法:(我做的项目的主要代码如下:)

////////////////////////////////////login.jsp//////////////////////////////////////

<html>

<head>

<title>登录页面</title>

</head>

<body >

<form id="form1" name="form1" method="post" action="login">///要有

  <table width="995" height="110" border="0" align="center" bordercolor="#FFCC99">

    <tr bordercolor="#0099FF">

      <td colspan="2" ><div align="center"><span class="STYLE2">  苍穹JAVA学习论坛</span></div></td>

      </tr>

    <tr height="30"></tr>

    <tr>

      <td width="183"></td>

      <td width="413"><table width="432" border="0" height="253">

        <tr bgcolor="#FFCC99">

          <td colspan="2"><div align="center"><span class="STYLE7">用户登录界面</span></div></td>

          </tr>

        <tr bgcolor="#FFCC99">

          <td width="134" height="62"><div align="right"><span class="STYLE8">用户名:</span></div></td>

          <td width="269"><label>

           <input type="text" name="UserName" " />

             <span class="STYLE10">还没注册?</span></label></td>

        </tr>

        <tr bgcolor="#FFCC99">

          <td height="72"><div align="right"><span class="STYLE8">   码:</span></div></td>

          <td><label>

            <input type="text" name="UserPwd" />

            <span class="STYLE10">忘记密码?</span>          </label></td>

        </tr>

        <tr bgcolor="#FFCC99">

          <td height="72" colspan="2"><label>

            <div  align="center">

<input name="login" type="submit" class="STYLE11" value="登录"  />             

  

             <input name="reset" type="reset" class="STYLE11" value="重置" />

            </div>

 </form>

  </body>

</html>

//////////////////////////LoginServlet.java///////////////////////////////////

 

public class LoginServlet extends HttpServlet {

 

public LoginServlet() {

super();

}

public void destroy() {

super.destroy();

}

      public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

          doPost(request,response);

}

 

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

 

response.setContentType("text/html");

response.setCharacterEncoding("utf-8");

PrintWriter out = response.getWriter();

        

        String name =request.getParameter("UserName");//获得用户名

        String pwd=request.getParameter("UserPwd");//获取密码

        HttpSession session=request.getSession(true);////sessionservlet的使用  

        User user=null;

UserBiz userbiz=new UserBiz();

        //检查用户名与密码

try {

user=userbiz.login(name, pwd);

} catch (Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

        try {

               if(user!=null){

session.setAttribute("userN",user.getUserName());///注意大小写 ///session 的设置

 

response.sendRedirect("user_index.jsp");

 

}else{

 

response.sendRedirect("index.jsp");

}

} catch (RuntimeException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

        out.flush();

out.close();

}

 

public void init() throws ServletException {

// Put your code here

}

}

///////////////////////////////user_index.jsp//////////////////////

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" session="true"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<body >

 <label></label>

  <p> </p>

  <table width="1315" height="96" border="0">

    <tr bordercolor="#0099FF">

      <td width="260" bgcolor="#FFCC99"></td>

      <td width="782" bgcolor="#FFCC99"><div align="center"><span class="STYLE2">苍穹JAVA学习论坛</span></div></td>

      <td width="397" bgcolor="#FFCC99"><div align="right"><a href="login.jsp">登录</a> | <a href="register.jsp">注册</a> </div></td>

    </tr>

    <tr height="30">

    <td colspan="3"><span></span><span ></span><span ></span><br></td>

    </tr>

    <tr>

     <% 

     String name=null;

     if( session.getAttribute("userN")!=null)///注意大小写

       {

       name=(String) session.getAttribute("userN");/////获得session

       }else{

       name="游客";

       }

     %>

   <td>欢迎您<%=name%></td>

      <td>主页面</td>

      <td> </td>

    </tr>

    

  </table>

  

</body>

</html>

 

0 0