电影网站: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. iOS10--消息通知的基本使用

    官方将通知单独放在了UserNotifications.framework,使用时需要导入框架.UserNotifications.framework主要类文件: UNCalendarNotifica ...

  2. TMS Xdata Server

    Xdata 在TMS中扮演的桥的角色,一年前仔细看过TMS 的源码,当时对流程很清晰,随着时间慢慢的过去,现在该忘记的都忘记了.所以用此文章来记录自己对Xdata还剩下的一点点的记忆... 光有xda ...

  3. python 3.x 实现简单用户登录

    import os import sys import getpass login_username = 'admin' login_password = ' u = 0 while u < 3 ...

  4. VIM - visual selection 模式下的简单操作

    1. 概述 vim 的 visual selection 模式下的简单操作 2. visual selection 模式 概述 可视化选择 可视化选择 vim 的一种专门用来选择的模式 可以提供相对于 ...

  5. 查询mssql的死锁语句

    都是从网上找的,只是记录一下,可能用到. 查询死锁,要在当前数据库下,否则tableName列得不到正确信息select    request_session_id spid,OBJECT_NAME( ...

  6. import require

    https://www.zhihu.com/question/56820346/answer/150743994

  7. PDO访问方式操作数据库

    mysqli是专门访问MySQL数据库的,不能访问其它数据库.PDO可以访问多种的数据库,它把操作类合并在一起,做成一个数据访问抽象层,这个抽象层就是PDO,根据类操作对应的数据库.mysqli是一个 ...

  8. 洛咕 P2463 [SDOI2008]Sandy的卡片

    哈希水过. 首先这是一段delta相同的序列,按照套路差分一下,b[i]=a[i]-a[i-1],然后就是这些序列的最长公共子段 由于数据范围很小,就可以二分,枚举第一个序列的子段然后每个子序列暴力c ...

  9. 手把手带你打造一个 Android 热修复框架(上篇)

    本文来自网易云社区 作者:王晨彦 前言 热修复和插件化是目前 Android 领域很火热的两门技术,也是 Android 开发工程师必备的技能. 目前比较流行的热修复方案有微信的 Tinker,手淘的 ...

  10. python-我的第一门编程语言

    一.认识python是一个偶然,由于大学不务正业,混迹于各种电脑维修群(本人专业商务经济专业),了解过C.JAVA.HTML5以及世界上最好的编程语言PHP and so on!了解也仅仅是了解. 二 ...