一个简单的小例子,基于httpservlet的用户登录(杂家是菜鸟刚接触javaee,只能从最简单出发了),首先看业务逻辑:
有三个文件组成,分别是登录、验证和欢迎界面。要达到的目标是:
下图是HttpServlet处理doGet和doPost请求的异同:
废话不说了,先看源码:
第一:Login.java
//登陆界面 package yan.guoqi; import javax.servlet.http.*; import java.io.*; public class Login extends HttpServlet{ public void doGet(HttpServletRequest req, HttpServletResponse res){ //业务逻辑 try{ //中文乱码解决 res.setContentType("text/html;charset=gbk"); PrintWriter pw = res.getWriter(); //返回登陆页面 pw.println("<html>"); pw.println("<body>"); pw.println("<h1>登陆界面</h1>"); pw.println("<form action=logincl method=post>"); pw.println("用户名:<input type=text name=username><br>"); pw.println("密码:<input type=password name=passwd><br>"); pw.println("<input type=submit value=登陆><br>"); pw.println("</form>"); pw.println("<body/>"); pw.println("<html/>"); } catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req, HttpServletResponse res){ this.doGet(req, res); } }
第二:LoginCL.java,验证用户名和密码是否正确
//用户验证servlet package yan.guoqi; import javax.servlet.http.*; import java.io.*; public class LoginCL extends HttpServlet{ public void doGet(HttpServletRequest req, HttpServletResponse res){ //业务逻辑 try{ //接受用户名和密码 String u = req.getParameter("username"); String p = req.getParameter("passwd"); //验证 if(u.equals("yan") && p.equals("yan")){ //合法 //跳转到welcome res.sendRedirect("wel"); } else{ //不合法 //跳转 res.sendRedirect("login");//你要跳转的servlet的url } } catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req, HttpServletResponse res){ this.doGet(req, res); } }
第三:Wel.java 如果验证成功,显示欢迎界面
//欢迎界面 package yan.guoqi; import javax.servlet.http.*; import java.io.*; public class Wel extends HttpServlet{ public void doGet(HttpServletRequest req, HttpServletResponse res){ //业务逻辑 try{ res.setContentType("text/html;charset=gbk"); PrintWriter pw = res.getWriter(); pw.println("Welcome,恭喜您,已成功登陆!"); } catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req, HttpServletResponse res){ this.doGet(req, res); } }
第四web.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <!-- ISO-8859-1 --> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description> <servlet> <!--给survlet起个名字,可以是任意的 --> <servlet-name>login</servlet-name> <!--servlet的路径(包名+类名) --> <servlet-class>yan.guoqi.Login</servlet-class> </servlet> <servlet-mapping> <!-- servlet的名字和上面保持统一 --> <servlet-name>login</servlet-name> <!-- 这是在浏览器中输入的访问该survlet的url,任意的 --> <url-pattern>/login</url-pattern> </servlet-mapping> <servlet> <!--给survlet起个名字,可以是任意的 --> <servlet-name>logincl</servlet-name> <!--servlet的路径(包名+类名) --> <servlet-class>yan.guoqi.LoginCL</servlet-class> </servlet> <servlet-mapping> <!-- servlet的名字和上面保持统一 --> <servlet-name>logincl</servlet-name> <!-- 这是在浏览器中输入的访问该survlet的url,任意的 --> <url-pattern>/logincl</url-pattern> </servlet-mapping> <servlet> <!--给survlet起个名字,可以是任意的 --> <servlet-name>wel</servlet-name> <!--servlet的路径(包名+类名) --> <servlet-class>yan.guoqi.Wel</servlet-class> </servlet> <servlet-mapping> <!-- servlet的名字和上面保持统一 --> <servlet-name>wel</servlet-name> <!-- 这是在浏览器中输入的访问该survlet的url,任意的 --> <url-pattern>/wel</url-pattern> </servlet-mapping> </web-app>
总结知识点:
1,页面的跳转用res.sendRedirect("xxx")来实现,xxx是xml文件里要跳转的servlet对应的url名字。
2,接收用户名和密码用String u = req.getParameter("username");还实现。其中的username是pw.println("用户名:<input type=text name=username><br>");这里对应的名字。
3,servlet里嵌入html语言。比如Login.java里的下面语句:
pw.println("<html>");
pw.println("<body>");
pw.println("<h1>登陆界面</h1>");
pw.println("<form action=logincl method=post>");
pw.println("用户名:<input type=text name=username><br>");
pw.println("密码:<input type=password name=passwd><br>");
pw.println("<input type=submit value=登陆><br>");
pw.println("</form>");
pw.println("<body/>");
pw.println("<html/>");
看来还要学习下html语言啊!
4,servlet解决中文乱码的方法:
res.setContentType("text/html;charset=gbk");
5,每个.java文件都要编译。对应的servlet文件在web.xml文件里都要配置注册下,这点很像android开发的Manifinest.xml文件。每次更改.java文件时,按韩顺平老师的讲解是reload一下。但有时候reload一下不中,把tomcat关闭重新开,浏览器关闭再输入网址打开,这样才看到更改后的效果。
-----------------------------下次这个验证模块要连上数据库,这个算小demo吧!大牛就别看源码了。Java web长路漫漫啊。。。。
源码:http://download.csdn.net/detail/yanzi1225627/5053157 下载后参照http://blog.csdn.net/yanzi1225627/article/details/8564455在相应位置新建文件夹,对应的文件放进去。