JAVA WEB开发从数据库中查询到的数据用list怎么在JSP页面整齐的显示出来,请写代码,
发布日期:2021-11-09 22:50:40 浏览次数:54 分类:技术文章

本文共 3676 字,大约阅读时间需要 12 分钟。

整齐地输出可以用table。

不知道你用的是原始的jsp还是struts之类的框架,如果是后者,有相应的标记库,直接绑定就可以了。

再不会google些例子程序自己学习下。
整齐地输出可以用table。

不知道你用的是原始的jsp还是struts之类的框架,如果是后者,有相应的标记库,直接绑定就可以了。

再不会google些例子程序自己学习下。

标准标签方式
页面头部引入<@ taglib uri="" prefix="c" /> 

有好多年不搞Java Web开发了,这几天正好国庆放假,放松之余也有兴趣回头看看Java Web开发技术的基础。

我们都知道,Servlet是Java Web开发的重要基础,但是由于Servlet开发相对繁琐,代码量庞大而且不易维护,美工无法参与界面设计开发等不足,于是就诞生了jsp。jsp是对servlet开发模型的重要升级。有了jsp,Java Web开发技术才真正被广泛使用。

一、Servlet

在Java Web开发当中,新建一个类继承(派生)自HttpServlet类即可创建一个Servlet。

比如:

[java] 
  1. @WebServlet(name="firstServlet",urlPatterns={
    "/firstServlet"})  
  2. public class FirstServlet extends HttpServlet {  
  3.   
  4.     /** 
  5.      * Constructor of the object. 
  6.      */  
  7.     public FirstServlet() {  
  8.         super();  
  9.     }  
  10.   
  11.     /** 
  12.      * Destruction of the servlet. <br> 
  13.      */  
  14.     public void destroy() {  
  15.         super.destroy(); // Just puts "destroy" string in log  
  16.         // Put your code here  
  17.     }  
  18.   
  19.     /** 
  20.      * The doGet method of the servlet. <br> 
  21.      * 
  22.      * This method is called when a form has its tag value method equals to get. 
  23.      *  
  24.      * @param request the request send by the client to the server 
  25.      * @param response the response send by the server to the client 
  26.      * @throws ServletException if an error occurred 
  27.      * @throws IOException if an error occurred 
  28.      */  
  29.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  30.             throws ServletException, IOException {  
  31.   
  32.         response.setContentType("text/html");  
  33.         PrintWriter out = response.getWriter();  
  34.         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");  
  35.         out.println("<HTML>");  
  36.         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");  
  37.         out.println("  <BODY>");  
  38.         out.print("    This is ");  
  39.         out.print(this.getClass());  
  40.         out.println(", using the GET method");  
  41.         out.println("  </BODY>");  
  42.         out.println("</HTML>");  
  43.         out.flush();  
  44.         out.close();  
  45.     }  
  46.   
  47.     /** 
  48.      * The doPost method of the servlet. <br> 
  49.      * 
  50.      * This method is called when a form has its tag value method equals to post. 
  51.      *  
  52.      * @param request the request send by the client to the server 
  53.      * @param response the response send by the server to the client 
  54.      * @throws ServletException if an error occurred 
  55.      * @throws IOException if an error occurred 
  56.      */  
  57.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  58.             throws ServletException, IOException {  
  59.   
  60.         response.setContentType("text/html");  
  61.         PrintWriter out = response.getWriter();  
  62.         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");  
  63.         out.println("<HTML>");  
  64.         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");  
  65.         out.println("  <BODY>");  
  66.         out.print("    This is ");  
  67.         out.print(this.getClass());  
  68.         out.println(", using the POST method");  
  69.         out.println("  </BODY>");  
  70.         out.println("</HTML>");  
  71.         out.flush();  
  72.         out.close();  
  73.     }  
  74.   
  75.     /** 
  76.      * Initialization of the servlet. <br> 
  77.      * 
  78.      * @throws ServletException if an error occurs 
  79.      */  
  80.     public void init() throws ServletException {  
  81.         // Put your code here  
  82.     }  
  83.   
  84. }  
这段代码是在myeclipse工具当中创建servlet时自动生成的,可以看到我们新建的FirstServlet类继承自HttpServlet。而且又实现了里面的doGet(Get请求时调用的方法)、doPost(Post请求时调用的方法)、init(初始化对象)、destroy(销毁对象)。值得注意的是我们采用了Annotation的方式进行了修饰,即:@WebServlet(name="firstServlet",urlPatterns={"/firstServlet"})。这是servlet3.0开始推荐的,以前采用的方式是在web.xml里面添加servlet和servlet-mapping配置。比如:

[html] 
  1. <servlet>  
  2.     <servlet-name>firstServlet</servlet-name>  
  3.     <servlet-class>包名.FirstServlet</servlet-class>  
  4. </servlet>  
  5. <servlet-mapping>  
  6.     <servlet-name>firstServlet</servlet-name>  
  7.     <url-pattern>/firstServlet</url-pattern>  
  8. </servlet-mapping>  

上面的配置有几点需要说明:

1、servlet里面的servlet-class节点需要填写servlet类的完整名称(包名.类名)

2、servlet-mapping下的servlet-name节点的名称要与servlet下的servlet-name节点的名称一致,才能找到。

3、url-pattern下的url要加"/"

启动tomcat,在ie中输入localhost:8080/工程名/firstServet,即可浏览该页面(Get请求)。8080端口是tomcat默认设置的端口号,可以在tomcat/conf/下面的server.xml文件中修改端口号。比如我们修改了默认端口号为8088,重新启动tomcat(加载xml)以后,访问的url即为:localhost:8088/工程名/servlet映射的urlpattern名。

转载地址:https://blog.csdn.net/Danlinan/article/details/45485469 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:6.2.3.2 BLOB 和 TEXT 类型
下一篇:jsp页面传中文到action中乱码问题

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月17日 05时57分32秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章