参考:
http://www.cnblogs.com/mengdd/archive/2013/06/11/3131936.html
http://blog.csdn.net/jiuqiyuliang/article/details/36875217
http://www.cnblogs.com/smyhvae/category/634173.html
MyEclipse环境配置
首先,安装一个MyEclipse,然后进行一些相关的环境配置(Window->Preferences):
比如字体、Formatter等。
也可以从Eclipse中导出配置,然后在MyEclipse中导入。
这里需要特别注意的是两个配置:
1.JSP的打开方式:
选为用编辑器打开:
Window->Preferences->General->File Associations
然后在右边窗口选jsp,下面选择MyEclipse JSP Editor,在右边按下Default按钮将其选为默认。
2.编码格式的选择
因为默认的格式不支持中文,所以需要将默认的格式改为UTF-8,这样就不用为了支持中文每次修改。
在Window->Preferences中搜索JSP,在左边点JSP,找到编码设置,设置为UTF-8:
新建程序
新建一个Web程序,File->New->Web Project.
起一个名叫HelloWeb,然后选6.0,点击Next:
切记勾选:deployment descriptor
之后项目就建成:
文件目录
打开index.jsp,可以修改里面的正文.
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+
request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!--action中是web.xml中的url-pattern,(这里给出相对路径),这样,提交之后才能启动相应的Servlet程序-->
<form action="LoginServlet">
username: <input type="text" name="username"><br>
password: <input type="password" name="password"><br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
</body>
</html>
在项目:HelloWeb上右击->run as->MyEclipse Server Application
选择服务器点击:Next
添加项目到服务器点击Finish
第一个Servlet程序
Servlet是Java服务器端编程,不同于一般的Java应用程序,Servlet程序是运行在服务器上的,服务器有很多种,Tomcat只是其中一种。
在src中新建一个包hello,新建一个Servlet:LoginServlet.java
新建Servlet编写Servlet程序如下:
package hello;
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;
@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet {
/**
* 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 {
String username = request.getParameter("username");
String password = request.getParameter("password");
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("username:"+username.toString()+"password"+password.toString()+"</br>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
web.xml
web.xml叫做deployment descriptor,部署描述符。
打开web.xml,编写内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>HelloWeb</display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>hello.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern><!-- 注意前面要有斜线-->
</servlet-mapping>
</web-app>
启动服务器,及打开浏览器(Open in web browser)
管理服务器
当表单提交之后,servlet运行,显示账号和密码。
表单中,有这样一个属性:method,它的默认值是get,如果将它设置为post,则提交表单后,地址栏不会有用户名和密码的显示。
网友评论