开发j2ee还是用专门的java ee eclipse,自带了许多开发j2ee的插件,包括:

This package includes:

  • Data Tools Platform
  • Eclipse Git Team Provider
  • Eclipse Java Development Tools
  • Eclipse Java EE Developer Tools
  • JavaScript Development Tools
  • Maven Integration for Eclipse
  • Mylyn Task List
  • Eclipse Plug-in Development Environment
  • Remote System Explorer
  • Eclipse XML Editors and Tools

我用的ide是

Eclipse Java EE IDE for Web Developers.

Version: Kepler Service Release 1.

新建一个工程: c dynamic web project.

创建dynamic web project

eclipse本身的dynamic web project类似MyEclipse?的web project,

MyEclipse对Eclipse进行了扩展,如:web project可以添加一些开源的框架支持,比如Struts、Hibernate等等。
也就是说,web project是Myeclipse扩展后的项目,而dynamic web project是Eclipse自带的分类,在Myeclipse中,web project具有dynamic web project特性并具有一些方便开发的集成功能。

启动后:

可以看到有一个Dynamic web project 选项,在eclipse标准版里是没有的。

创建Server

通过菜单选择File > New > Other>Server,创建Server,如下图所示。

单击“下一步”,再单击“完成”。

二,创建Dynamic Web Project项目

1,通过菜单选择File > New > Dynamic Web Project,新建项目,项目名HelloWorld,其他值默认。

2, 项目资源如下图所示。

三,创建JSP文件

1,创建index.jsp文件,右击项目,New >JSP FILE,如下图所示,单击完成。

2,在<body></body>中间处插入如下代码:

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

<h1>Today's date is<%= d.toString()%></h1>

3,右击项目,Run...>Run on Server,选择刚新建的Server。运行效果如下图所示:

四,创建Servlet文件

1,新建HelloWorldServlet,右击项目,New >Servlet,如下图所示,单击完成。

(可以看到,servlet文件放在根目录下的src包中)

2,在doGet方法中添加如下代码:

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

3,运行Servlet,重新启动Server。运行效果如下:

说明:

1.Dynamic Web Project项目的WEB-INF目录下默认没有web.xml文件。除非勾选这个.

 

Server只是方便开发和调试WEB项目,真实布署WEB应用时,应该修改Tomcat安装目录下的conf/server.xml文件和Web端项目下的WEB-INF/web.xml文件。

用户登录例子

1.新建login.jsp

<%@ page language="java" contentType="text/html; charset=utf8"
pageEncoding="utf-8"%>
<!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>Login</title>
</head>
<body> <form method="POST" action="login"> <input type="text" name="username"/>
<input type="text" name="password"/>
<input type="submit" value="登陆"/> </form> </body>
</html>

注意我们的action

2.Servlet,在Web项目的src中右键新建一个类LoginAction 的servlet

package com.demo;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginAction extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public LoginAction() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest req, HttpServletResponse res)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath()); PrintWriter out=res.getWriter(); String username=req.getParameter("username");
String password=req.getParameter("password"); String ret;
if(username.equals("admin"))
{
if(password.equals("admin"))
{
ret="登陆成功";
}
else
{
ret="密码错误";
}
}
else
{
ret="用户名错误";
} //返回html页面
res.setContentType("text/html;charset=utf-8");
out.println("<html>");
out.println("<head>");
out.println("<title>登录信息</title>");
out.println("</head>");
out.println("<body>");
out.println("welcome欢迎【" + username + "】用户登录成功!!!");
out.println("</body>");
out.println("</html>"); out.flush();
out.close();
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

3.在WebRoot下的WEB-INF的web.xml,部署我们的Servlet,启动服务器。

  <servlet>
<servlet-name>login</servlet-name> <!-- 名字随便 -->
<servlet-class>com.demo.LoginAction</servlet-class><!-- servlet类名 -->
</servlet> <servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern> <!-- url虚拟访问路径,最后我们通过工程名/login进行访问,像这样http://127.0.0.1:8080/helloworld/login -->
</servlet-mapping>

4.一定要启动服务器,之后在浏览器中输入你的URL

http://localhost:8080/helloworld/login.jsp

上面中我们没有配置类似:*.jsp的访问格式也能够访问jsp页面,为什么?

tomcat的 conf目录下默认配置文件web.xml文件中有这个配置项,类似这样

    <!-- The mapping for the default servlet -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- The mappings for the JSP servlet -->
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>

可以看到,通过通配符*进行了映射,所以可以访问任意jsp文件。

参考:

http://blog.csdn.net/jiuqiyuliang/article/details/36424981

更多参考:

http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.stardust.docs.wst%2Fhtml%2Fwst-integration%2Fdynamic-web-proj.html 官方文档

http://blog.csdn.net/wangchenggong88/article/details/6830316

http://www.blogjava.net/ynstudio/archive/2009/07/01/284960.html

http://orajc.blog.51cto.com/458434/263296/     eclipse Java EE平台使用指南(一)

http://blog.sina.com.cn/s/blog_7ee821410101ipt6.html

Eclipse IDE for Java EE Developers使用和新建工程helloworld的更多相关文章

  1. Eclipse IDE for Java EE Developers 与 Eclipse Classic(Eclipse Standard)区别

    Eclipse下载官网:http://www.eclipse.org/downloads/ 版本: 1.Eclipse classic(Eclipse Standard):Eclipse的标准版; 2 ...

  2. eclipse ide for java ee developers 开发环境搭建(j2ee)

    转载自:http://www.iteye.com/topic/982182 真的是一片很不错的文章啊! 使用eclipse真的有年头了,相信java程序员没有不知道它的,最近在给团队中新来的应届生做指 ...

  3. eclipse ide for java ee developers 开发环境搭建(J2EE) 【转载】

    使用eclipse真的有年头了,相信java程序员没有不知道它的,最近在给团队中新来的应届生做指导,专门讲解了一下Eclipse开发环境的搭建过程, 一是帮助他们尽快的熟悉IDE的使用,二也是保证团队 ...

  4. Eclipse IDE for java EE Developers下载和安装

    1.登录 http://www.eclipse.org/home/index.php ,下载Eclipse IDE for java EE Developers 2.解压缩压缩包到任意路径(推荐:G: ...

  5. eclipse ide for java ee developers与eclipse ide for java developers有什么区别

    前者集成了WTP,可用于j2ee开发,功能更完善

  6. Eclipse IDE for C/C++ Developers安装配置详解

    Eclipse IDE for C/C++ Developers安装配置详解(转) 转自:http://hi.baidu.com/ltb6w/item/986532efd712460f570f1ddc ...

  7. ubuntu下安装eclipse IDE for C/C++ developers

     序 linux的GUI和windos比起来实在逊色,虽然它的终端模式(命令行模式)非常强大.linux发行版ubuntu的GUI相对其他版本要华丽一些,所以最近由redhat转向ubuntu进行li ...

  8. Linux下安装JRE和Eclipse IDE for C/C++ Developers

    Linux32位,下载eclipse-cpp-luna-R-linux-gtk.tar.gz和jre-8u11-linux-i586.rpm  放到家文件夹中. http://www.eclipse. ...

  9. Eclipse IDE for C/C++ Developers和MinGW安装配置C/C++开发学习环境详解

    Eclipse IDE for C/C++ Developers和MinGW安装配置C/C++开发学习环境详解 操作系统:Windows 7 JDK版本:1.6.0_33 Eclipse版本:Juno ...

随机推荐

  1. 《Programming WPF》翻译 第7章 4.转换

    原文:<Programming WPF>翻译 第7章 4.转换 支持高分辨率显示是WPF中的重要样式.这是被部分地支持--强调了可伸缩的向量图,而不是图像.但是,正如使用GDI+和GDI3 ...

  2. MySQL数据备份之mysqldump

      mysqldump常用于MySQL数据库逻辑备份 1.各种用法说明 A. 最简单的用法: mysqldump -uroot -pPassword [database name] > [dum ...

  3. ECharts 使用实例

    HTML与JavaScript代码: <%@ page language="java" contentType="text/html; charset=UTF-8& ...

  4. H Language Blueprint

    H Language Blueprint I will design the H language in the very-soon future, it will be like: 1- a scr ...

  5. [Immutable.js] Using fromJS() to Convert Plain JavaScript Objects into Immutable Data

    Immutable.js offers the fromJS() method to build immutable structures from objects and array. Object ...

  6. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问的解决方案

    今天写了一个excel表的导入功能,结果在excel表中的内容导入到页面时报错:SQL  Server 阻止了对组件 'Ad Hoc Distributed Queries' 的  STATEMENT ...

  7. 推荐几个常用的jquery ui 框架

    jQuery ui框架很多,除了官方提供的jquery UI(如果你还不知道什么是jQuery UI,请看下载了jquery ui后如何使用),还有很多第三方提供的ui框架,因官方提供的jquery ...

  8. mvc原理和mvc模式的优缺点

    一.mvc原理   mvc是一种程序开发设计模式,它实现了显示模块与功能模块的分离.提高了程序的可维护性.可移植性.可扩展性与可重用性,降低了程序的开发难度.它主要分模型.视图.控制器三层. 1.模型 ...

  9. UIDeviceOrientation UIInterfaceOrientation 区别

    UIDeviceOrientation      是机器硬件的当前旋转方向   这个你只能取值 不能设置 UIInterfaceOrientation   是你程序界面的当前旋转方向   这个可以设置 ...

  10. POJ 2112 Optimal Milking (二分 + 最大流)

    题目大意: 在一个农场里面,有k个挤奶机,编号分别是 1..k,有c头奶牛,编号分别是k+1 .. k+c,每个挤奶机一天最让可以挤m头奶牛的奶,奶牛和挤奶机之间用邻接矩阵给出距离.求让所有奶牛都挤到 ...