jsp中MVC模式——網路交友(二)出現問題了……

來源:互聯網
上載者:User

Servlet的對象找不到……哪位高手指點一下……(從註冊後提交就出現問題)

二、javabean和Servlet管理

         本系統javabean類的包名都是mybean.data;Servlet的包名都是myservlet.control

          javabean類和Servlet類的位元組碼分別複製到D:/Tomcat 6.0/webapps/mkfriend/WEB-INF/classes/mybean/data

           和D:/Tomcat 6.0/webapps/mkfriend/WEB-INF/classes/myservlet/control中

三、設定檔

        目錄:D:/Tomcat 6.0/webapps/mkfriend/WEB-INF

          web.xml

          <?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
       <servlet-name>register</servlet-name>
       <servlet-class>myservlet.control.HandleRegister</servlet-class>
</servlet>
<servlet-mapping>
      <servlet-name>register</servlet-name>
       <url-pattern>/helpRegister></url-pattern>
</servlet-mapping>
<servlet>
       <servlet-name>login</servlet-name>
       <servlet-class>myservlet.control.HandleLogin</servlet-class>
</servlet>
<servlet-mapping>
      <servlet-name>login</servlet-name>
       <url-pattern>/helpLogin</url-pattern>
</servlet-mapping>
<servlet>
       <servlet-name>upload</servlet-name>
       <servlet-class>myservlet.control.HandleUpload</servlet-class>
</servlet>
<servlet-mapping>
      <servlet-name>upload</servlet-name>
       <url-pattern>/helpUpload></url-pattern>
</servlet-mapping><servlet>
       <servlet-name>lookRecord</servlet-name>
       <servlet-class>myservlet.control.HandleDatabase</servlet-class>
</servlet>
<servlet-mapping>
      <servlet-name>lookRecord</servlet-name>
       <url-pattern>/helpShowMember></url-pattern>
</servlet-mapping>
<servlet>
       <servlet-name>modifyPassword</servlet-name>
       <servlet-class>myservlet.control.HandlePassword</servlet-class>
</servlet>
<servlet-mapping>
      <servlet-name>modifyPassword</servlet-name>
       <url-pattern>/helpModifyPassword></url-pattern>
</servlet-mapping>
<servlet>
       <servlet-name>modifyOldMess</servlet-name>
       <servlet-class>myservlet.control.HandleModifyMess</servlet-class>
</servlet>
<servlet-mapping>
      <servlet-name>modifyOldMess</servlet-name>
       <url-pattern>/helpModifyMess></url-pattern>
</servlet-mapping>
<servlet>
       <servlet-name>exit</servlet-name>
       <servlet-class>myservlet.control.HandleExit</servlet-class>
</servlet>
<servlet-mapping>
      <servlet-name>exit</servlet-name>
       <url-pattern>/helpExitLogin></url-pattern>
</servlet-mapping>
</web-app>
  

                                                              會員註冊

一、模型(javabean)

            Register.java描述使用者註冊資訊

                package mybean.data;
public class Register{
           String logname="",password="",email="",phone="",message="";
           String backNews;
           public void setLogname(String name){
                 logname=name;
                }
           public String getLogname(){
                 return logname;
               }
           public void setPassword(String pw){
                  password=pw;
                 }
            public String getPassword(){
                    return password;
              }
            public void setEmail(String em){
                     email=em;
              }
             public String getEmail(){
                      return email;
                    }
             public void setPhone(String ph){
                      phone=ph;
                    }
             public String getPhone(){
                      return phone;
                   }
              public void setMessage(String m){
                      message=m;
                     }
              public String getMessage(){
                       return message;
                    }
               public void setBackNews(String s){
                         backNews=s;
                     }
                public String getBackNews(){
                          return backNews;
                         }
       }

二、控制器(Servlet)

HandleRegister.java

package myservlet.control;
import mybean.data.*;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HandleRegister extends HttpServlet{
         public void init(ServletConfig config)throws ServletException{
                   super.init(config);
                  try{
                           Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                        }catch(Exception e){}
                    }
           public String handleString(String s){
                     try{
                             byte bb[]=s.getBytes("ISO-8859 -1");
                             s=new String(bb);
                      }
                      catch (Exception ee){}
                       return s;
                   }
         public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
                  Connection con;
                  Statement sql;
                  Register reg=new Register();
                  request.setAttribute("register",reg);
                  String logname=request.getParameter("logname").trim(),
                  password=request.getParameter("password").trim(),
                  email=request.getParameter("email").trim(),
                  phone=request.getParameter("phone").trim(),
                  message=request.getParameter("message");
                  String uri="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=MakeFriend";
                  if(logname==null)
                     logname="";
                  if(password==null)
                     password="";
                  boolean isLD=true;
                  for(int i=0;i<logname.length();i++){
                            char c=logname.charAt(i);
                            if(!((c<='z'&&c>='a')||(c<='Z'&&c>='A')||(c<='9'&&c>='0')))
                               isLD=false;
                             }
                  boolean boo=logname.length()>0&&password.length()>0&&isLD;
                  String backNews="";
                  try{
                        logname=handleString(logname);
                        password=handleString(password);
                        phone=handleString(phone);
                        email=handleString(email);
                        message=handleString(message);
                        String pic="public.jpg";
                        String insertRecord="('"+logname+"','"+password+"','"+phone+"','"+email+"','"+message+"','"+pic+"')";
                  con=DriverManager.getConnection(uri,"sa","sa");
                  String insertCondition="INSERT INTO member VALUES"+insertRecord;
                  sql=con.createStatement();  
                  if(boo){
                           int m=sql.executeUpdate(insertCondition);
                           if(m!=0){
                               backNews="註冊成功";
                               reg.setBackNews(backNews);
                               reg.setLogname(logname);
                               reg.setPassword(password);
                               reg.setPhone(phone);
                               reg.setEmail(email);
                               reg.setMessage(message);
                  }
               }
              else{
                     backNews="資訊填寫不完整或名字中有非法字元";
                     reg.setBackNews(backNews);
                      }
                     con.close();
                }
                catch(SQLException exp){
                            backNews="該會員名已被使用,請您更換名字";
                            reg.setBackNews(backNews);
                         }
             RequestDispatcher dispatcher=request.getRequestDispatcher("showRegisterMess.jsp"); 
                 dispatcher.forward(request,response);
      }
            public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
                       doPost(request,response);
                   }
}            
三、視圖(jsp)

register.jsp 註冊資訊頁面

<%@page contentType="text/html;charset=GB2312"%>
<head><%@include file="head.txt"%></head>
<html><body bgcolor=cyan><font size=2>
<center><form action="helpRegister" name=form method=post>
<table>
         輸入您的資訊,會員名字必須由字母和數字組成,帶*號的必須填寫。
<tr><td>會員名稱:</td><td><input type=text name="logname">*</td></tr>
<tr><td>設定密碼:</td><td><input type=password name="logname">*</td></tr>
<tr><td>電子郵件:</td><td><input type=text name="email">*</td></tr>
<tr><td>聯絡電話:</td><td><input type=text name="phone">*</td></tr>
</table>
<table>
 <tr><td><font size=2>輸入您的簡曆和交友標準:</td></tr>
<tr><td><textarea name="message" rows="6" cols="30"></textarea></td></tr>
 <tr><td><input type=submit name="g" value="提交"></td></tr>
</table></form></center></body></html>

showRegisterMess.jsp

<%@page contentType="text/html;charset=GB2312"%>
<%@page import="mybean.data.Register"%>
<jsp:useBean id="register" type="mybean.data.Register"  scope="request"/>
<head><%@include file="head.txt"%></head>
<html><body bgcolor=cyan>
<center>
 <font size=4 color=blue>
          <br><jsp:getProperty name="register" property="backNews"/>
    </font>
<table>
<tr><td>註冊會員名稱:</td>
         <td><jsp:getProperty name="register" property="logname"/></td>
</tr>
<tr><td>註冊會員電子郵件:</td>
          <td><jsp:getProperty name="register" property="phone"/></td>
</tr>
</table><tr><td>您的簡曆和交友標準:</td></tr>
               <tr><td><textarea name="message" rows="6" cols="30">
             <jsp:getProperty name="register" property="message"/>
             </textarea>
             </td></tr></table></center></body></html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.