【jsp】为什么出现JSP?

Java 发表评论

 首先,servlet写一个页面:

		package com.it.demo;
		
		import java.io.IOException;
		import java.io.PrintWriter;
		import javax.servlet.ServletException;
		import javax.servlet.http.HttpServlet;
		import javax.servlet.http.HttpServletRequest;
		import javax.servlet.http.HttpServletResponse;
		
		public class HtmlServlet extends HttpServlet {
			protected void doGet(HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
				response.setContentType("text/html;charset=UTF-8");
				PrintWriter writer = response.getWriter();
				//动态的响应html页面
				writer.write("<!DOCTYPE html>");
				writer.write("<html>");
				writer.write("<head>");
				writer.write("<meta charset='UTF-8'>");
				writer.write("<title>Insert title here</title>");
				writer.write("</head>");
				writer.write("<body>");
				writer.write("<h1>这个页面很繁琐</h1>");
				writer.write("</body>");
				writer.write("</html>");
				
			}
		
			protected void doPost(HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
				doGet(request, response);
			}
		}

效果:

                                                                                     

右击查看源代码:

        

         

    Servlet是java代码,JSP是java标签。用servlet编写页面很繁琐,需要注意和多余的代码很多(比如引号),并且它把页面代码和逻辑代码捆绑在一起,不易于维护。这么多繁琐的代码,引入jsp标签,便可轻松解决。

 

上一篇:

发表评论

邮箱地址不会被公开。 必填项已用*标注

昵称 *