Java教程

第八周

本文主要是介绍第八周,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.登陆
输入用户名密码,判断用户名和密码相同,登陆成功,session中保存用户的用户名,进入主页main.jsp,主页有一个退出按钮,点击,回到登陆页login.jsp。要求:退出登录后,如果在浏览器直接输入主页main.jsp,访问不了,直接跳到登陆页。

2.购物车
和上一题一起,在main.jsp中做一个购物车,里面显示3个商品名和价格 每一个后面有一个加入购物车按钮,main.jsp中有一个按钮(或者超链接)可以显示购物车。(选作:在购物车中加删除按钮删除商品)

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登录</title>
  </head>
  <body>
      <script type="text/javascript">
          function validate(){
              if(loginForm.uname.value == ""){
                  alert("账号不能为空!");
                  return;
              }
              if(loginForm.upwd.value == ""){
                  alert("密码不能为空!");
                  return;
              }
              loginForm.submit();
          }
      </script>
    <form action="2.jsp" name="loginForm" method="post">
        <table  border="0"  cellpadding="10">
            <tr><td>用户名:</td>
                <td><input type="text" name="uname"></td>
            </tr>
            <tr><td>密码:</td>
                <td><input type="password" name="upwd"></td>
            </tr>
            <tr>
                <td><input type="button" value="登录" onclick="validate()"></td>
            </tr>
        </table>
    </form>

  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  
    <title>登录Two</title>
  </head> 
  <body>
  <%
    request.setCharacterEncoding("UTF-8");
    String uname = request.getParameter("uname");
    String upwd = request.getParameter("upwd");
    if(upwd.equals(uname)){
        session.setAttribute("uname", uname);
        request.getRequestDispatcher("3.jsp").forward(request, response);
    }else{
        
         response.sendRedirect("index.jsp");
    }
    
 %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String uname = (String) session.getAttribute("uname");
    if (uname == null) {
        response.sendRedirect("index.jsp");
    } else {

        out.print("欢迎" + uname + "登录!");
    }
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
    <a href="index.jsp">退出登录</a>
    <form action="5.jsp" method="post">
        <table border="1" width="600px" height="300px">
            <thead>
                <tr>
                    <th>商品名称</th>
                    <th>价格</th>
                    <th>加购</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>橙子</td>
                    <td>5</td>
                    <td><input type="checkbox" name="gw" value="橙子"></td>
                </tr>
                <tr>
                    <td>西瓜</td>
                    <td>2</td>
                    <td><input type="checkbox" name="gw" value="西瓜"></td>
                </tr>
                <tr>
                    <td>苹果</td>
                    <td>1</td>
                    <td><input type="checkbox" name="gw" value="苹果"></td>
                </tr>
                <tr>
                    <td colspan="3"><input type="submit" value="加购"></td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
  </head>
  <body>
      <%session.invalidate();%>
      <script type="text/javascript">
          window.location.href="index.jsp";
      </script>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
  </head>
  <body>
      <h2>购物车:</h2>
        <%
          request.setCharacterEncoding("UTF-8");
          String[] gw = request.getParameterValues("gw");
          if(gw == null){
              out.print("购物车为空!");
          }else{
              for(int i = 0; i < gw.length; i++){
                  out.print(gw[i]+"<br>");
              }
          }
       %>
  </body>
</html>

这篇关于第八周的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!