电影网站:www.aikan66.com

项目网站:www.aikan66.com
游戏网站:www.aikan66.com
图片网站:www.aikan66.com
书籍网站:www.aikan66.com
学习网站:www.aikan66.com
Java网站:www.aikan66.com
iOS网站:www.aikan66.com

----

新建web项目,名字web06-PanduanLogin

servlet PortalServlet中,判断访问用户是否已经登录,如果没有,将请求转发给LoginServlet2;

如果已经登录,则调用RequestDispacher接口的forward()方法,将请求转发给WelcomeYou,显示欢迎。

新建servlet,名字PortalServlet.java,

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter(); //接收前台传过来的参数user和password
String name = request.getParameter("user");
String pwd = request.getParameter("password"); if("ultra".equals(name)&&"ultra".equals(pwd))
{
//如果已经登录,则调用RequestDispatcher接口的forward()方法,将请求转发给Welcome
ServletContext context=getServletContext();
RequestDispatcher rd=context.getRequestDispatcher("/welcome");
rd.forward(request, response);
}else{
//如果没有登录,调用RequestDispatcher接口的include()方法,将请求转发给login2
RequestDispatcher rd = request.getRequestDispatcher("login2");
rd.include(request, response);
}
out.close();
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doGet(request, response);
}

----

新建servlet,名字LoginServlet2

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<form method=post action=portal>");
out.println("<talbe");
out.println("<tr>");
out.println("<td>请输入用户名:</td>");
out.println("<td><input type=text name=user></td>");
out.println("</tr>"); out.println("<tr>");
out.println("<td>请输入密码:</td>");
out.println("<td><input type=password name=password></td>");
out.println("</tr>"); out.println("<tr>");
out.println("<td><input type=reset value=重填></td>");
out.println("<td><input type=submit value=登录></td>");
out.println("</tr>"); out.println("</table>");
out.print("</form>"); out.close();
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request,response);
}

----

新建servlet,名字Welcome

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException { String user=req.getParameter("user");//获取到前台的name:user文本框中的值;前台过来的叫请求req。
String welcomeInfo="Welcome you,"+user;//字符串的拼接 resp.setContentType("text/html"); PrintWriter out=resp.getWriter();
out.println(welcomeInfo);
out.close(); } /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("dopost"); this.doGet(req, resp);
}

----

配置web.xml

  <servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>PortalServlet</servlet-name>
<servlet-class>PortalServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginServlet2</servlet-name>
<servlet-class>LoginServlet2</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>welcome</servlet-name>
<servlet-class>WelcomeYou</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PortalServlet</servlet-name>
<url-pattern>/portal</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet2</servlet-name>
<url-pattern>/login2</url-pattern>
</servlet-mapping>

----

浏览器中访问 http://localhost:8080/web06-PanduanLogin/portal

访问结果

如果已经登录

如果没有登录

web06-PanduanLogin的更多相关文章

  1. Myeclipse+AJAX+Servlet

    最近刚开始学AJAX的知识,这里介绍一个简单的Myeclipse+AJAX+Servlet项目. 此项目包含3个文件:index.jsp.check.java.还有一个需要配置的文件是:web.xml ...

  2. JavaWeb学习记录(九)——Cookie的增加、删除、查看

    一.servlet功能代码: public void doGet(HttpServletRequest request, HttpServletResponse response)           ...

  3. hyper中安装wdOS-1.0-x86_64(wdlinux)遇到的网卡问题

    11/23 0:30 by vmaxhyper中安装wdOS-1.0-x86_64(wdlinux) 日志: 遇到的问题: 1.装完找不到网卡eth0,只有一只loopback的Lo. 原因:cent ...

  4. memcached 缓存数据库应用实践

    1.1 数据库对比 缓存: 将数据存储到内存中,只有当磁盘胜任不了的时候,才会启用缓存   缺点:断电数据丢失(双电),用缓存存储数据的目的只是为了应付大并发的业务. 数据库: mysql(关系型数据 ...

  5. 关于TomCat上传文件中文名乱码的问题

    最近在学习TomCat文件上传这一部分,由于文件上传必须要三个条件: 1.表单提交方式必须为Post 2.表单中需要有<input type="file">元素,还需要 ...

  6. docker container(容器)

    docker 容器 Docker容器类似于一个轻量级的沙箱,Docker利用容器来运行和隔离应用 容器是从镜像创建的应用运行实例.它可以启动,开始,停止,删除,而这些容器都是彼此相互隔离,互不可见的. ...

  7. ServletContextListener中的方法contextInitialized执行了两次

    有一个web06项目是直接拷贝web05的,复制过后web06项目默认的web配置中的Context Root还是web05,导致tomcat在启动时还是会创建两个web应用,修改成web06后,cl ...

  8. 2-jsp简介

    一.什么是jsp:1.只能运行在服务器中2.可以将java代码嵌入html页面中的技术 补充:在eclipse中修改jsp的创建模板 window--preference--web--jsp file ...

  9. 005.Docker存储管理

    一 Docker volume形态 因为Docker 采用 AFUS 分层文件系统时,文件系统的改动都是发生在最上面的容器层,在容器的生命周期内,它是持续的,包括容器在被停止后.但是,当容器被删除后, ...

  10. ansible使用1

    常用软件安装及使用目录   ansible软件2 ### ansible软件部署安装需求#### 01. 需要有epel源 系统yum源(base epel--pip gem) sshpass---e ...

随机推荐

  1. ubuntu下安装eclipse IDE for C/C++ developers

     序 linux的GUI和windos比起来实在逊色,虽然它的终端模式(命令行模式)非常强大.linux发行版ubuntu的GUI相对其他版本要华丽一些,所以最近由redhat转向ubuntu进行li ...

  2. jQuery----淘宝商品展示(类似与tab切换)

    实现效果如图:                 功能需求: ①鼠标进入商品名称,商品名称变色,同时对应的物品展示图片显示对应的物品,鼠标移出时候,商品名称恢复原来的颜色 实现分析: 1.HTML+CS ...

  3. 在 Mac 上通过 Docker 运行 Asp.net Core 简易教程

    首先,你需要在 Mac 上安装好 Docker 下载地址 https://download.docker.com/mac/stable/Docker.dmg 或者查看别人的 安装教程 怎么安装这里就不 ...

  4. Mac编译安装swfTools

    Mac编译安装swfTools Mac编译安装swfTools 下载源码 解压源码 安装依赖项 ./configure执行配置或者执行如下配置命令: 需要注意这里存在部分代码错误,需要修改解决后执行 ...

  5. webpack / vue项目 config/index.js配置(用于配置webpack服务器代理)

    'use strict' // Template version: 1.1.3 // see http://vuejs-templates.github.io/webpack for document ...

  6. 使用C语言给php写扩展

    1.在php源码路径的ext文件夹下,新建一个extend_test.def文件,编辑文件内容为 string my_test_function(string str,int n) 2.在当前目录执行 ...

  7. 通过R语言统计考研英语(二)单词出现频率

    通过R语言统计考研英语(二)单词出现频率 大家对英语考试并不陌生,首先是背单词,就是所谓的高频词汇.厚厚的一本单词,真的看的头大.最近结合自己刚学的R语言,为年底的考研做准备,想统计一下最近考研英语( ...

  8. Scribe+HDFS日志收集系统安装方法

    1.概述 Scribe是facebook开源的日志收集系统,可用于搜索引擎中进行大规模日志分析处理.其通常与Hadoop结合使用,scribe用于向HDFS中push日志,而Hadoop通过MapRe ...

  9. 洛谷 P1306 斐波那契公约数

    洛谷 P1306 斐波那契公约数 题目描述 对于Fibonacci数列:1,1,2,3,5,8,13......大家应该很熟悉吧~~~但是现在有一个很“简单”问题:第n项和第m项的最大公约数是多少? ...

  10. wpf 中Listbox获取选中的值

    布局代码: <ListBox  Name="listBox1" Width="120" Height="52"  SelectionC ...