⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 complete_1.java

📁 程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件
💻 JAVA
字号:
package questions.c17;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Complete_1 extends HttpServlet {
   public void doGet( HttpServletRequest req, 
                      HttpServletResponse res ) 
         throws ServletException, IOException
   {
      PrintWriter out = res.getWriter( );
      res.setContentType( "text/html" );
      out.println( "<HTML>" );
      out.println( "<BODY>" );
      out.println( "<H3>Change Password</H3>" );
      out.println( "<p>Before you set a password, " +
        "your initial password is \"<b>password</b>\". " +
        "Passwords must be 5 to 8 characters long.</p>" );
      out.println( "<pre>" );
      out.println( " <FORM METHOD=POST " +
         "http:localhost:8080/" +
         "servlet/questions/c17/Complete_1> " );
      out.println( "<p>Old password............" +
         "<INPUT NAME=old TYPE=password WIDTH=8 " +
         "VALUE=\"\"></p>" );
      out.println( "<p>New password............" +
         "<INPUT NAME=new1 TYPE=password WITDH=8 " +
         "value=\"\"></p>" );
      out.println( "<p>Confrim new password...." +
         "<INPUT NAME=new2 TYPE=password WITDH=8 " +
         "VALUE=\"\"></p>" );
      out.println( "</pre>" );
      out.println( "<p><INPUT TYPE=SUBMIT " +
         "VALUE=\"Change Now\"></p>" );
      out.println( "</FORM>" );
      out.println( "</BODY>" );
      out.println( "</HTML>" );
   } 
   public void doPost( HttpServletRequest req, 
                     HttpServletResponse res ) 
         throws ServletException, IOException
   { try {
         HttpSession session = req.getSession( false );
         String oldpw, newpw1, newpw2;  
         String password;    
         if ( session == null ) { 
            password = "password"; 
            session = req.getSession( true );  
            session.putValue( "password", password );
         } else {
            password = (String)
               session.getValue( "password" );
         }
         oldpw = (String) req.getParameter( "old" ); 
         if ( ! password.equals( oldpw ) ) {
            throw new PasswordException(
               "Old password not correct" );
         }
         newpw1 = (String) req.getParameter( "new1" ); 
         if ( ( newpw1.length() < 5 ) ||
              ( newpw1.length() > 8 ) ) {
            throw new PasswordException( 
               "Passwords must have 5 to 8 characters." );
         }
         newpw2 = (String) req.getParameter( "new2" );
         if ( newpw2.equals( newpw1 ) ) {
            session.putValue( "password", newpw1 );
         } else {
            throw new PasswordException( 
               "The new passwords do not match." );
         }
        confirm( res, "Password changed." );
      } catch ( PasswordException e ) {
         confirm( res, "Pasword not changed:<br>" +
            e.getMessage() );
      }
   } 
   private void confirm( HttpServletResponse res, String msg ) 
        throws ServletException, IOException   
   {  PrintWriter out = res.getWriter( );
      res.setContentType( "text/html" );
      out.println( "<HTML>" );
      out.println( "<BODY>" );
      out.println( "<p>" + msg + "</p>" );
      out.println( 
         "<p>Press <b>Back</b> to try again.</p>" );
      out.println( "</BODY>" );
      out.println( "</HTML>" );
   } 
} 
class PasswordException extends Exception {
   PasswordException() {
      this( "Password error" );
   }
   PasswordException( String msg ) {
      super( msg );
   }
}
    

           
      

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -