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 ...
随机推荐
- C++中数组名和指针的区别联系
原文:http://www.cnblogs.com/ddx-deng/archive/2012/12/16/3755862.html 第一个结论: #include "iostream.h& ...
- JavaWeb基础—过滤器Filter
一.概念 JavaWeb三大组件之一(组件都有一个特性,需要在web.xml中配置) 过滤器:会在一组资源(jsp servlet等)的前面执行,可以让请求得到目标资源,也可以终止请求,不再继续 也就 ...
- 2015306 白皎 《网络攻防》Exp3 免杀原理与实践
2015306 白皎 <网络攻防>Exp3 免杀原理与实践 一.实践基础 免杀,故名思义,指的是一种能使病毒木马免于被杀毒软件查杀的技术. 免杀的方法有很多,比如加壳改壳.加垃圾指令.以及 ...
- jQuery学习- 内容选择器
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 菜鸟vimer成长记——第4.2章、编程插件
简介 这部分的插件是与编程相关的插件.主要涉及两大块:所有编程语言通用的插件,以及各个语言独有的插件.插件的数量和合理性,这可能是一个不断累积和修正的过程. 个人感觉,现在比较适合sh,html,ma ...
- 菜鸟vimer成长记——第2.0章、模式初探
首先,其他的文本编辑器只有一种模式,就是插入模式.而vim一下子颠覆了我们的世界观——有好多模式.这个是思维上的切换,很难也很重要!!! 其次,Vim 提供一个区分模式的用户界面.也就是说在不同的模式 ...
- Android Studio 重命名文件
选中文件,Refactor——Rename AS提供了两种方式重命名,Rename 和 Rename File,暂时还不清楚区别.有知道的麻烦告知下.
- Spring学习(十八)----- Spring AOP+AspectJ注解实例
我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...
- ThinkPHP学习笔记(一)----初识ThinkPHP
在做微信开发的时候原本使用来yii框架,后续觉得yii虽然功能强大使用方便,但是整个框架太大了,不适合一些轻量级的开发:这个时候发现thinkphp这个框架,框架本身很小,只有几M,但麻雀虽小,但五脏 ...
- Android Service(下)
阅读本文需要先阅读Android Service(上) 一 为什么需要bindService() 绑定服务就是为了和服务进行通讯 可以调用服务里面的方法 二 bindService()调用服务里面方法 ...