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

 我做的项目遇到的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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值