您的位置:首页 > 编程语言 > Java开发

毫无废话: 从0开始一点一滴用java开发自己的B/S--3.2

2004-05-17 17:37 459 查看
再给一个例子:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

建立工程giveYou(不要忘了WEB-INF和她的孩子们)
在giveYou下写三个文件
1:login.jsp内容如下
<%@ page contentType="text/html; charset=gb2312"%>
<html>
       <form method="post" action="http://localhost:8080/giveYou/Test2">
       <center>
              <table width=32%  border=0>
              <tr>
                    <td align=right width=12% >username:</td>
                    <td>
                           <input type=text name=username size=20  >
                    </td>
             </tr>
             <tr> 
                    <td align=right width=12%>password:</td>
                    <td>
                           <input type=password name=password size=20 >
                    </td>
             </tr>
             <tr>  
                    <td width=12%>
                           <input type=submit name=submit value=submit>
                    </td>
                    <td>
                           <input type=reset name=reset value=reset >
                    <td>
             </tr>
             </table>
        </center>
        </form>
</HTML>
2:success.jsp内容如下

<%@ page contentType="text/html; charset=gb2312"%>

<html>

       <head>

              <title>error</title>

              <meta http-equiv="content-type" content="text/html; charset=gb2312">

       </head>

       <body>

              <h1><font color=green>Success!!</font></h1>

              <form mothod="post" action="login.jsp">

                     <input type="submit" name="submit" value="返回">

              </form>

       </body>

</html>

3:error.jsp内容如下

<%@ page contentType="text/html; charset=gb2312"%>

<html>

       <head>

              <title>error</title>

       </head>

       <body>

              <h1><font color=red>Error!!你没有输入用户名、密码</font></h1>

              <form mothod="post" action="login.jsp">

                     <input type="submit" name="submit" value="返回">

              </form>

       </body>

</html>

在Eclipe中新建你的工程吧,也取名叫giveYou(把你的源文件(.java)放在myThree--- javaSource里,把你的类文件(.class)放在myThree--- WEB-INF--- classes里面),不要忘了导入servlet.jar
写一个类Test,内容如下:
import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class Test extends HttpServlet

{

private String userId,passWord;  

   public void doPost( HttpServletRequest rq,

                     HttpServletResponse rp )

                     throws IOException,ServletException

    {

       rp.setContentType("text/html;charset=gb2312");

       PrintWriter out=rp.getWriter();

        userId=rq.getParameter("username");

       passWord=rq.getParameter("password");

       if((userId!=null)&&(passWord!=null))

       {

              rp.sendRedirect("/giveYou/success.jsp");

       }

       else

       {

              rp.sendRedirect("/giveYou/error.jsp");

        }

    }

}

由于web.xml与myThree中的一样,在此就不说了
请再次启动你的tomcat
在你的ie地址栏输入: http://localhost:8080/giveYou /login.jsp
 

本章后语:到了这里的时候,你一定已经很不耐烦了,认为我的“毫无废话”成了“废话连篇”,但是,我的哥哥姐姐,到了上面的这个例子,你认为你是不可以把它写的稍微复杂,稍微漂亮一些了呢,不要让页面老是那么苍白、不要让文字总是那么的黑不咙咚、做几十个类,互相调用看看、在doPost()中写个条件语句,根据获得的不同值跳转到不同的页面试试,相信你的程序再不会那么孤独了。
一点早该跟你说的话:你应该注意到了,每个应用都放在tomcat的webapps下面,每个应用都必须有WEB-INF结构(你不要跟我说你没注意到这一点,那我将为你看了这么多的废话感到悲哀)。为什么呢?因为服务器tomcat启动的时候,她靠什么来判断哪些应用该她“管辖”呢。她就在自己的webapps文件夹下面,找到所有的文件夹,并且看这些文件夹是不都有WEB-INF文件夹,如果有,她就会去看看WEB-INF下面有没有web.xml这个文件。如果webapps下的任何一个文件夹没有WEB-INF结构,她会认为这个东西与她无关。tomcat自动的读取web.xml中的内容,当你运行你的程序需要相关的内容的时候,她就看看你写的web.xml是否提供了相关的配置。---但愿我说清楚了!!! :)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐