首页 > 代码库 > 用jsp写注冊页面

用jsp写注冊页面

包含单选框、多选框、session的应用,页面自己主动跳转,中文乱码的处理,入门级

对于中文乱码的处理,注意几点:注冊页面数据提交方式为post不能忘了写,页面编码方式为gbk,处理提交信息的doRegister.jsp要加

request.setCharacterEncoding("gbk");一定要加!

注冊页面


register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
  <head>
    <title>注冊页面</title>
  </head>
  <body>
  <center>
    <h3>欢迎注冊该站点</h3>
    <form action="doRegister.jsp" method="post">
       <table>
          <tr>
            <td>username</td>
            <td><input type="text" name="user"></td>
          </tr>
          <tr>
            <td>password</td>
            <td><input type="password" name="pwd">password不得少于6位</td>
          </tr>
          <tr>
            <td>性别</td> 
            <td><input type="radio" name="sex" value=http://www.mamicode.com/"男">男>处理注冊信息页面

doRegister.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
  <head>
    <title>处理用户注冊信息</title>
  </head>
  <body>
  <h3>保存用户信息</h3>
  <hr>
    <%
       request.setCharacterEncoding("gbk");
       String user=request.getParameter("user");
       String pwd=request.getParameter("pwd");
       if(user.equals("")||pwd.equals("")){
         out.println("username或password不能为空!请又一次注冊");
         response.setHeader("refresh", "3;url=register.jsp");
       }
       else if(pwd.length()<6){
         out.println("password不能少于6位!请又一次注冊");
         response.setHeader("refresh", "3;url=register.jsp");
       }
       else{
       session.setAttribute("user",user);
       session.setAttribute("pwd",pwd);
       String xingbie=request.getParameter("sex");
       session.setAttribute("sex", xingbie);
       String[] str=(String[])request.getParameterValues("checkbox1");
       session.setAttribute("zhuanchang", str);     
       response.sendRedirect("showRegisterInfo.jsp");
       }
      %>  
  </body>
</html>
注冊成功,显示信息页面


showRegisterInfo.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
  <head>
    <title>显示用户注冊信息</title>
  </head>  
  <body>
    <h3>恭喜你,注冊成功</h3>
    <h3>你的注冊信息为:</h3>
    <%
      String username=(String)session.getAttribute("user");
      out.println("用户名:"+" "+username+"<br>");
      String password=(String)session.getAttribute("pwd");
      out.println("密码:"+" "+password+"<br>");
      String sex=(String)session.getAttribute("sex");
      out.println("性别:"+" "+sex+"<br>");
      String[] zhuanchang=(String[])session.getAttribute("zhuanchang");
      out.print("擅长的语言:");
      for(int i=0;i<zhuanchang.length;i++)
        out.print(" "+zhuanchang[i]);
     %>
  </body>
</html>

用jsp写注冊页面