web06-PanduanLogin
项目网站: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的更多相关文章
- Myeclipse+AJAX+Servlet
最近刚开始学AJAX的知识,这里介绍一个简单的Myeclipse+AJAX+Servlet项目. 此项目包含3个文件:index.jsp.check.java.还有一个需要配置的文件是:web.xml ...
- JavaWeb学习记录(九)——Cookie的增加、删除、查看
一.servlet功能代码: public void doGet(HttpServletRequest request, HttpServletResponse response) ...
- hyper中安装wdOS-1.0-x86_64(wdlinux)遇到的网卡问题
11/23 0:30 by vmaxhyper中安装wdOS-1.0-x86_64(wdlinux) 日志: 遇到的问题: 1.装完找不到网卡eth0,只有一只loopback的Lo. 原因:cent ...
- memcached 缓存数据库应用实践
1.1 数据库对比 缓存: 将数据存储到内存中,只有当磁盘胜任不了的时候,才会启用缓存 缺点:断电数据丢失(双电),用缓存存储数据的目的只是为了应付大并发的业务. 数据库: mysql(关系型数据 ...
- 关于TomCat上传文件中文名乱码的问题
最近在学习TomCat文件上传这一部分,由于文件上传必须要三个条件: 1.表单提交方式必须为Post 2.表单中需要有<input type="file">元素,还需要 ...
- docker container(容器)
docker 容器 Docker容器类似于一个轻量级的沙箱,Docker利用容器来运行和隔离应用 容器是从镜像创建的应用运行实例.它可以启动,开始,停止,删除,而这些容器都是彼此相互隔离,互不可见的. ...
- ServletContextListener中的方法contextInitialized执行了两次
有一个web06项目是直接拷贝web05的,复制过后web06项目默认的web配置中的Context Root还是web05,导致tomcat在启动时还是会创建两个web应用,修改成web06后,cl ...
- 2-jsp简介
一.什么是jsp:1.只能运行在服务器中2.可以将java代码嵌入html页面中的技术 补充:在eclipse中修改jsp的创建模板 window--preference--web--jsp file ...
- 005.Docker存储管理
一 Docker volume形态 因为Docker 采用 AFUS 分层文件系统时,文件系统的改动都是发生在最上面的容器层,在容器的生命周期内,它是持续的,包括容器在被停止后.但是,当容器被删除后, ...
- ansible使用1
常用软件安装及使用目录 ansible软件2 ### ansible软件部署安装需求#### 01. 需要有epel源 系统yum源(base epel--pip gem) sshpass---e ...
随机推荐
- 【VSC】.txt文件打开乱码
我在拉取项目之后打开其备注文件查看,发现里面的中文乱码,在网上寻找解决方案自定义设置并没有什么作用. 回到文件目录用记事本打开,在里面随意输入中文,保存. 之后———— 恩,vscode中的该文本同步 ...
- 2015306 白皎 《网络攻防》Exp1 进阶
2015306 白皎 <网络攻防>Exp1 进阶 Task1 64位shellcode的编写及注入 - 自己编写一个64位shellcode.参考shellcode指导. - 自己编写一个 ...
- 26-[jQuery]-内容补充
jquery除了咱们上面讲解的常用知识点之外,还有jquery 插件.jqueryUI知识点 jqueryUI 官网: https://jqueryui.com/ jqueryUI 中文网: http ...
- 【FJOI2016】建筑师
安利另外一篇\(blog\) 密码泥萌都知道 题面 题解 为了描述方便,这里将建筑称作\(zsy\) 高度为\(n\)的\(zsy\)无论如何都能从左右两侧看到.剩下的部分,从左边看到的是前缀\(ma ...
- 微信小程序——手把手教你写一个微信小程序
前言 微信小程序年前的跳一跳确实是火了一把,然后呢一直没有时间去实践项目,一直想搞但是工作上不需要所以,嗯嗯嗯嗯嗯emmmmm..... 需求 小程序语音识别,全景图片观看,登录授权,获取个人基本信息 ...
- 接口测试之基础篇--http协议
概念:超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTTP最初的目的是为了提供一种 ...
- Linux之linux入门
学习linux之前先了解一下操作系统: 操作系统的定义: 操作系统(英语:operating system,缩写作 OS)是管理计算机硬件与软件资源的计算机程序,同时也是计算机系统的内 ...
- charles抓包https/模拟弱网/设置断点重定向/压测
charles几个常用功能 1,ios 抓包https网页:(如未配置,会显示unknown) 第一步是:给手机安装SSL证书 手机和电脑在同一wifi下,手机wifi配置http代理,ip是电脑 ...
- flask的cookie和session的简单原理
在Flask的框架中,自己已经封装了 cookie的respons,request 有存储就有读取及删除,那么就拿购物车来举例 在我们登陆的时候会有之前在购物车存放的物品.也就是说在一个地方为我们保存 ...
- 六边形地图Cube coordinates理解
1.这个是 Axial coordinates,可以实现六边形4个方向上的移动 2.但是六边形还有两个方向需要移动,所以引入了Cube coordinates,这个坐标系多了一个轴向,Y轴,X轴沿水平 ...