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 ...
随机推荐
- jquery animate() Alternate 语法
前段时间在使用jQuery的animate() 函数时候用到Alternate方式.主要是要让数字自增到指定大小,且能看见数字增加过程. 一般使用如下方式: function autoPlusAnim ...
- 基于 HTML5 WebGL 的计量站三维可视化监控系统 Web 组态工控应用
得益于 HTML5 WebGL 技术的成熟,从技术上对工控管理的可视化,数据可视化变得简单易行!完成对工控设备的管理效率,资源管理,风险管理等的大幅度提高,同时也对国家工业4.0计划作出有力响应! 如 ...
- starshot常见问题(New)
Element组件网址: http://element-cn.eleme.io/#/zh-CN/component/message Layer组件网址: https://www.layui.com/d ...
- hadoop环境搭建-伪分布模式
Appache hadoop 版本:2.77 jdk:1.8 系统:centos7 注意不要在root下解压,要单独建一个用户安装hadoop及其组件. 一.先查看系统是否有自带j #dk: r ...
- sqli-labs学习(less-5-less-7)
先介绍一些函数 count(*) 返回在给定的选择中被选的行数,即结果的数目 报错了,但是union没有出结果?,只是为什么? 原来是这样,这样的话只能用报错注入了 (). 通过floor报错 and ...
- 关于VC++6.0与WIN10系统不兼容的解决办法
记得第一次接触C语言,用的第一个编译器就是VC++6.0.当时自己的是Win10系统,第一次安装就打不开,后来网上一查说是系统兼容性的问题.今天室友突然想安装VC++6.0,也遇到了兼容的问题,我就帮 ...
- IIC总线(集成电路总线)
三大串行总线:UART.SPI.IIC(其中SPI是由时钟沿采集数据,为同步接口:UART和IIC是由电平采集数据,为异步接口) IIC速率:工作在半双工方式,2根线(SCL和SDA) 标准:100k ...
- 【转】JavaScript操作SVG的一些知识
原文:http://blog.iderzheng.com/something-about-svg-with-javascript/ 前阵子学习了一下SVG(Scalable Vector Graphi ...
- 【HNOI2013】比赛
题面 题解 \(n \leq 9 \to\)爆搜 对每一场的结果进行搜索,最后进行\(\mathrm{check}\) 然后会发现没有什么分 搜索最重要的就是剪枝 接下来就列出一些剪枝 搜索时,强制每 ...
- 传输层tcp协议以及scoket套字节方法
一.传输层 1.传输层的由来: 网络层的IP帮我们区分子网 以太网的Mac帮我们找到主机 所以通过IP和Mac找到了一台特定的主机 如何找到该特定主机的应用程序呢? 答案是通过端口,端口即应用程序与网 ...