Ultimate Edition Servlet-- I can only prompt you do not miss passing

Ultimate Edition Servlet

Introduction: two days saw SSM framework, wanted to continue to learn later, the forehead of a turn back, can not just go quietly, watching so many days of Servlet, write the last one be goodbye, this named ultimate Edition, is the highest level I can understand, dedicated to the needs of friends who want to help, if big brother passing, forgive us, a rookie still on the road .

Understanding Servlet

Think of our JavaSE learning process, from the simplest of constants, variables, data types to function, array, object-oriented tools and then learning all the way through the clutter and kill over the invincible, but found busy at the university for six months, but in the console output has been, alas Calls Xi, almost high-risk zai! Programming is difficult, extremely difficult! Haha, in fact, that these words of a friend, not a difficult program, but you have not found the beauty of the program, want to see beautiful scenery, we must "list of small hills", the same, in order to discover the beauty of programming, you have to "list of US programming "thought the decision height, this time you want the total output in the console, what does that mean, what the browser is not a programmer to write out of it, that when talented something new. If you really think so, do not worry, no matter what you have to understand the needs, in order to fly the browser, look at the blog post " B / S and C / S " popularize knowledge. Knowing finished, I'll explain to you, in order to be associated with the browser, you have to learn Serlvet, not why, because Servlet is to communicate the front and rear of the bridge (a little skin, and do not know what it count wrong sentences , ha ha), you think, you enter a URL in the browser address bar, such https://www.cnblogs.com/zyx110/ , this is what you send a request to the server in the browser, then you I discovered your browser page to jump to my garden blog page (is so magical, more try to increase popularity points), which is after you send your request to the server to server response, and this process is the legendary the completion of the Servlet, Servlet is responsible for requesting data, processes the request, complete response, so Servlet we know the importance of it.

 

Learn Servlet

After reading the above paragraph that you should understand Servlet it (understand a hair, the doors are not into it), here I'll teach you how to learn Servlet. When it comes to learning, I shall not repeat it, " eight law " an important sex, learning to learn should not die, since all know that interest is the best teacher, then why not develop their own interest in it, and then grant you a " teach you to fall in love with programming sister ," I'm learning methods, technical knowledge are shared with you, next to you myself. I only guide you need to master to learn that 20%, in the form of questions before I write it, and then you learn the route, you take the issue to learn:

How to achieve Servlet?

What is JavaWeb four domain?

How to understand the forwarding and redirection?

 

Let me talk about the first question, there are three ways to achieve Servlet

 

 

 

To see how they implement " Servlet study ," I'm thinking mainly about this method say, do not speak of specific knowledge, knowledge is dead, wrote this white paper waste, the rookie tutorial full of knowledge, to check on the line, thinking is the most important, because it needs its own to enlightenment, grow up very few people will tell you this, I also listen to other people tell me, his insight, but I want to share my harvest to others, hoping to Some friends help.

 

好,进入主题,知道如何实现Servlet后,了解Servlet的生命周期,这个自己看,然后不知道大家看没看过一些玄幻小说,玄幻小说中有许多中都有空间戒指,空间袋等用来存储东西的,就拿空间戒指说吧,不知道有没有看过《斗破苍穹》的朋友,这部小说里就有空间戒指,叫做“纳戒”,此物内部自成空间,能容纳很多东西,戴在手上就跟普通戒指一样。为什么要提这个“纳戒”,废话,当然跟学习有关了,要是想看小说,就看看我写的小说《匹马行天下》,不仅能丰富你的精神世界,还能增长知识,何乐而不为呢(其实是想拉点粉,哈哈,记得关注哦)。再回到戒指,之前提到了JavaWeb四大域,这个域有点抽象,所以你先理解为空间戒指,Servlet有一个戒指叫ServletContext,里面能存放东西,这还不是神奇的,最神奇的是存到这个“戒指”里的东西不仅自己能拿,别的Servlet都能共享,,同一个服务器下的所有Servlet都能存和取“戒指”里的东西,怎么样,够神奇吧。具体在“戒指”里是怎样存东西和取东西的还是看博客《进一步理解Servlet》.

 

好,明白了JavaWeb四大域,再聊聊别的,你看你在点浏览器页面上的那些链接的时候,是不是页面会一跳一跳的,很神奇,不信你可以再试着点点这个https://www.cnblogs.com/zyx110/(哈哈,淡定,别打我),是不是又跳到我的博客园页面了。说重点,这种跳转也是有名字的,一种叫转发,另一种叫重定向。两个都是跳转,名字不一样,功能也是不一样的,我大概说两种区别:

 

首先要知道,转发在跳转的时候带的东西(参数)能被他要跳转的页面接收到,也是用“空间戒”带的东西,只不过这个“空间戒”的名字叫Request域,而重定向在跳转的时候,他可以在“空间戒”里装东西,但是他要跳的页面不能通过Request域(空间戒)接收他带的东西,他如果非要让他带的东西让别人接收的话可以通过ServletContext域(空间戒),看破不说破,让重定向留点隐私,你们自己去看吧。

 

还有一点要知道,就是转发只请求一次服务器,而重定向请求两次。怎么理解,我举个例子,准备好,我要开车了。知道我的都知道,我是个大二的学生,咳咳,先做个自我介绍,我叫***,男孩,性格活泼开朗,爱交朋友,爱学习,没女朋友(那个,,,先就这吧,编不下去了),我用我自己举个例,假如说我毕业后要领毕业证,本来很高兴的事,不想东窗事发,由于在校期间挂科补考没过,学校不给发毕业证。怎么办,我记得像热锅上的蚂蚁,大山里走出来的孩子要是拿不上毕业证,那不得让村里人笑话,情急之下我开始找关系,注意了,现在分两种情况:

情节一:

我费了九牛二虎之力找到了一位主任(是谁我也不知道,故事需要吧,先假想一个,现实很残酷的,哪有这么容易找到的),这个主任就是负责这块的,我也不能空着手去吧,拿出毕生心血买了两条中华烟再加两瓶茅台(这就是参数,拿这些应该差不多了吧),好,找到某主任跟他求情,主任一看我态度不错,主要还知道他喜欢喝两口,买酒了,就答应了,说我得跟校长说一下才行。我说好的好的,谢谢主任。然后主任拿着我送的中华烟和茅台酒,回家藏到柜子里了,然后拿着过年没吃完剩下的两条黑兰州去找校长,校长好说话,主任一说就答应了,毕竟还是要给主任点面子的。那这事算成了,不就我就得到通知,说来学校领毕业证,然后就证书到手了。

 

来,分析故事(纯属虚构,别当真),这个过程中不管中间是如何周转的,我是不是只找了一次主任,然后事成后不管是校长说的还是主任说的,总之我得到了一个消息,这就是转发,只请求一次服务器,中华烟、茅台、黑兰州是我带的参数,怎样,理解了吧。

 

 

情节二:

我还是费劲九牛二虎之力找到了某主任,但我说了,生活不总是一帆风顺的,这不,带着烟酒(参数)去找某主任,可主任不吃这套,给拒绝了,但我也不好把送进去的东西再带走吧,一咬牙一跺脚,就送他了,然后灰溜溜的回去了,到家后再找,看看有没有跟情节一一样的主任,这是后话了。

分析故事:从带烟酒(参数)找主任被拒绝,回来后再找别的主任,这整个过程我跑了两次,这就相当于你向服务器发出请求,然后服务器给你响应说访问不了,你再去重新请求,然后你又发出请求跳到一个页面上,这就是重定向。

 

 

 

故事补充:情节二中你再去找别的主任,之前买的东西没有了吧,虽然故事中是把东西留第一个主任那了,回归到知识点的话,这有个知识点,就是Request域的生命周期是只存在于一次请求,所以等到重定向第二次请求服务器的时候,参数已失效。

具体学习转发和重定向看博客《重定向和转发是什么垃圾》。

 

补充:再补充一点,故事纯属虚构,我还没挂过科,在这提示并希望学弟学妹们在真实生活中千万不要挂科,千万不要,千万!说三遍,很重要!!!

 

 

认识JSP

前面介绍并学习了Servlet,认真看了我推荐的博客的朋友肯定知道我们能通过Servlet向浏览器页面输出HTML样式,但是虽然可以输出,但是写着很麻烦,代码量很大,容易出错,就像下面图中所示的一样,这时候就到了JSP闪亮登场的时候了。

 

 

 

JSP不仅成功解决了页面显示代码量大的困难,而且里面还能写Java代码并执行。功能也算是很强大了。之前我提到了,JSP是Servlet,其底层就是用Servlet写的,可以看看源码,如何看源码和如何知道JSP是Servlet,看《教你看JSP源码》,知道了这些之后就看如何去学习JSP,关于JSP的知识我之前写的很全,就不在这写了,看我博客就行《JSP学习一》  ,《JSP学习二》。

 

 

认识JSTL和EL表达式

JSP固然强大,可是也有不足,在写Java语句和HTML语句的时候,嵌套在一起书写也是很麻烦,看下图

 

 

这样写是不是感觉也很麻烦,这时候JSTL和EL表达式也都闪亮登场,EL表达式语法简单,使用方便,用${  }代替了<%=  %>,简化了书写,还有JSTL,一些大牛们自己封装了一套代码,然后以标签的形式让我们使用,实现快速开发,在JSP很火的时候,许多公司都有自己的标签库,都是自己封装的,这样就提高了开发效率,因为我们可以自定义标签,可以自己封装代码,把一些常用的功能,比如说分页,封装成标签,经过一次封装后,下次不光能自己用,也能让别人用,许多代码只需几个标签快速实现,你的能力有多大,舞台就有多大。

 

 

我靠,纯手写3500字了,真能说,我不说了,好累好困,大家都在过暑假,而我却比在学校还忙,希望越努力越幸运吧!

 

是时候跟Servlet、 JSP正式告别了,以后我可能不会再写Servlet了,至少目前这个暑假应该不会了,别人都说过时的JSP我却研究了这么久,唉,,,,,,不知道有没有意义,不说了,先这样吧,此篇完,欲知后事如何,请看下回分解。

 

 

*****************************************************************************************************

我的博客园地址:https://www.cnblogs.com/zyx110/

转载请说明出处

我不能保证我所说的都是对的,但我能保证每一篇都是用心去写的,我始终认同“分享的越多,你的价值增值越大”,欢迎大家关注我的技术分享“Java匹马行天下”和学习心得分享“匹马行天下”,在分享中进步,越努力越幸运,期待我们都有美好的明天!

支持我的朋友们记得点波推荐哦,您的肯定就是我进步的动力。

 

 

Guess you like

Origin www.cnblogs.com/zyx110/p/11271820.html