Java Web应用开发实例

来源:互联网 发布:怎么找淘宝套现商铺 编辑:程序博客网 时间:2024/06/10 16:27

上次提到了Eclipse+Tomcat+Lomboz Java Web开发环境的配置,可环境配置好了,如何进行web应用的开发呢?index.html,**.jsp 等文件应该放到什么地方,servlet类如何建立?……都是问题。下面两个小例子算作是web应用开发的入门。

1.         HelloWorld例子(看到这个例子大家都熟悉吧,下面就看用网页如何实现打印HelloWorld

首先建立工程,在包资源管理器空白处点击右键,选择新建->其他,在打开的对话框中,选择Web分支Dynamic Web Project,点击下一步按钮,填写Project name栏(我们工程的名字为WebTest),点击完成按钮,建立项目。

Eclipse会提示你打开J2EE透视图,选择确定。

然后建立server,在J2EE透视图右下方,有一个Servers的视图(如果没有,可以通过窗口->显示视图->其他,在打开的对话框中选择Server分支选择Servers视图,打开视图),点击右键,选择新建->服务器,在弹出的对话框中选择Apache分支Tomcat v5.0 Server,点击下一步,在弹出对话框的左侧窗口选择WebTest项目,点击添加,将其添加到右侧窗口,点击完成

完成后,Servers视图中会添加一个tomcat的服务器,点击右键,选择Start,启动tomcat服务器

WebTest项目上点击右键,选择新建->JSP,在弹出的对话框中填写文件名,点击完成,建立JSP文件。我们建立的文件名为index.jsp

JSP文件的源码为下:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

    "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>My Title</title>

</head>

<body>

<% java.util.Date d = new java.util.Date(); %>

<h1>

Today's date is <%= d.toString() %> and this jsp page worked!

</h1>

</body>

</html>

写完后保存,Tomcat服务器的Status会显示为Restart,右键点击服务器,选择Restart->StartTomcat服务器重启。

打开IE输入http://localhost:8080/WebTest

就会显示当前的时间,每次刷新都会显示新的时间。

2.         Servlet的例子(通过建立Servlet的例子,看看servlet应该如何建立)

先如第一个例子建立一个工程,TomcatTest,在工程上点击右键,选择新建->Servlet

在弹出的对话框中,填写Java packageClass nameSuperclass点击完成建立ServletHelloServlet

然后建立index.html文件

内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=GB18030">

<title>helloapp</title>

</head>

<body>

<p><font size "7">Welcom to HelloApp</font></p>

<p><href="login.jsp?language=English">English version</a>

</body>

</html>

建立login.jsp

内容:

<%@ page language="java" contentType="text/html; charset=GB18030"

    pageEncoding="GB18030"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=GB18030">

<title>helloapp</title>

</head>

<body>

<br>

<form name="loginForm" method="post" action="HelloServlet">

<table>

<tr>

<td><div align="right">User Name:</div></td>

<td><input type="text" name="username"></td>

</tr>

<tr>

<td><div align="right">Password:</div></td>

<td><input type="password" name="password"></td>

</tr>

<tr>

<td></td>

<td><<input type="Submit" name="Submit" value="Submit"></td>

</tr>

</table>

</form>

 

</body>

</html>

建立hello.jsp

内容:

<%@ page language="java" contentType="text/html; charset=GB18030"

    pageEncoding="GB18030"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=GB18030">

<title>helloapp</title>

</head>

<body>

<b>Welcome:<%= request.getAttribute("USER"%></b>

</body>

</html>

建立HelloServlet

内容:

package com.example.servlets;

 

import java.io.IOException;

 

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

/**

 * Servlet implementation class for Servlet: HelloServlet

 *

 */

 public class HelloServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

        private String target = "/hello.jsp";

    /**

        *

        */

       private static final long serialVersionUID = -3522462295690035558L;

 

       /* (non-Java-doc)

        * @see javax.servlet.http.HttpServlet#HttpServlet()

        */

       public HelloServlet() {

              super();

       }         

      

       /* (non-Java-doc)

        * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

        */

       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

              response.getWriter().write("Hello, world!");

              doPost(request,response);

       }   

      

       /* (non-Java-doc)

        * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

        */

       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

              String username = request.getParameter("username");

              String password = request.getParameter("password");

             

              request.setAttribute("USER", username);

              request.setAttribute("PASSWORD", password);

             

              ServletContext context = getServletContext();

             

              System.out.println("Redirecting to" + target);

              RequestDispatcher dispatcher = context.getRequestDispatcher(target);

              dispatcher.forward(request,response);

       }                    

}

完成后将项目加入Tomcat服务器,重启服务器,在IE中输入http://localhost:8080/TomcatTest

可以进行测试

看看Servlet是否生效。

0 0
原创粉丝点击