您的位置:首页 > 数据库

JAVA WEB开发从数据库中查询到的数据用list怎么在JSP页面整齐的显示出来,请写代码,

2015-05-04 20:25 1036 查看
整齐地输出可以用table。

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

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

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

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

标准标签方式

页面头部引入<@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" />

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

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

一、Servlet

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

比如:

[java] view
plaincopy





@WebServlet(name="firstServlet",urlPatterns={"/firstServlet"})

public class FirstServlet extends HttpServlet {

/**

* Constructor of the object.

*/

public FirstServlet() {

super();

}

/**

* Destruction of the servlet. <br>

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

/**

* The doGet method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to get.

*

* @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 doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("<HTML>");

out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");

out.println(" <BODY>");

out.print(" This is ");

out.print(this.getClass());

out.println(", using the GET method");

out.println(" </BODY>");

out.println("</HTML>");

out.flush();

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 {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("<HTML>");

out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");

out.println(" <BODY>");

out.print(" This is ");

out.print(this.getClass());

out.println(", using the POST method");

out.println(" </BODY>");

out.println("</HTML>");

out.flush();

out.close();

}

/**

* Initialization of the servlet. <br>

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}

}

这段代码是在myeclipse工具当中创建servlet时自动生成的,可以看到我们新建的FirstServlet类继承自HttpServlet。而且又实现了里面的doGet(Get请求时调用的方法)、doPost(Post请求时调用的方法)、init(初始化对象)、destroy(销毁对象)。值得注意的是我们采用了Annotation的方式进行了修饰,即:@WebServlet(name="firstServlet",urlPatterns={"/firstServlet"})。这是servlet3.0开始推荐的,以前采用的方式是在web.xml里面添加servlet和servlet-mapping配置。比如:

[html] view
plaincopy





<servlet>

<servlet-name>firstServlet</servlet-name>

<servlet-class>包名.FirstServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>firstServlet</servlet-name>

<url-pattern>/firstServlet</url-pattern>

</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名。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐