JavaEE入门手把手教你做简单登陆界面(1)原始的jsp+servlet

该系列随笔只是简单的为大家演示一下JavaEE从最基本的jsp+servlet到struts1,struts2,jsf框架类的登陆界面,全部是最简单的用户密码验证(不连数据库),为的只是记录小弟的学习历程和让一些摸不着头脑的朋友对JavaEE进行“爱的初体验”,而后激发自己专研的兴趣,还请各位路过的大神请勿嘲笑,也请各位多多指教,该系列随笔所使用到的工具如下:

Java JDK1.6

Tomcat6.0:  http://www.apache.org/dist/tomcat/tomcat-6/v6.0.36/bin/apache-tomcat-6.0.36.zip

Eclipse:http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/juno/SR1/eclipse-jee-juno-SR1-win32.zip

 

关于工具的配置,这里就不多做叙述,Google或者百度一下,很多的说明,简单易懂。(PS:养成良好的搜索和查询API习惯对今后学习有莫大的帮助,没事多百度,有事请Google)。

 

今天,先为大家做一个最原始的jsp页面+servlet的登陆页面。

 

第一,在eclipes里建立一个Dynamic Web Project,如图,

 

这一页可以配置jsp的相关版本和服务器,在这里我选择了tomcat6.0

 

第二,选中你建立好的工程new一个servlet,这里我取名为MyServlet,copy下面的代码到你的servlet里。

 1 import java.io.IOException;
 2 import java.io.PrintWriter;
 3 
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 /**
10  * Servlet implementation class MyServlet
11  */
12 public class MyServlet extends HttpServlet {
13     private static final long serialVersionUID = 1L;
14        
15     /**
16      * @see HttpServlet#HttpServlet()
17      */
18     public MyServlet() {
19         super();
20         // TODO Auto-generated constructor stub
21     }
22 
23     /**
24      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
25      */
26     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
27         response.setContentType("text/html;charset=utf-8");
28         String username = request.getParameter("username");//getParameter里的参数是页面上name中定义的名字.
29         String password = request.getParameter("password");//同上
30         PrintWriter out = response.getWriter();
31         out.println("<html><head></head><body>");//打印html标签
32         
33         // 进行判断
34         if(username.equals("admin")&&password.equals("admin")){
35             out.println("login successful!");
36         }else{
37             out.println("login failed!");
38         }
39         
40         out.println("</body></html>");//打印闭合标签
41     }
42 
43     /**
44      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
45      */
46     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
47         doGet(request,response); //调用post方法也直接用get方法中的内容
48     }
49 
50 }

 

第三,打开工程中的web.xml文件,你可以看到,eclipse已经将你刚才所建的servlet相关信息配置完成了,如若没有自动完成配置,可按照下面的内容进行手动配置。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>JSP</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   <servlet>
13     <description></description>
14     <display-name>MyServlet</display-name>
15     <servlet-name>MyServlet</servlet-name>
16     <servlet-class>MyServlet</servlet-class>
17   </servlet>
18   <servlet-mapping>
19     <servlet-name>MyServlet</servlet-name>
20     <url-pattern>/MyServlet</url-pattern>
21   </servlet-mapping>
22 </web-app>

第四,建立index页面,填充如下代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    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=UTF-8">
<title>This is Demo</title>
</head>
<body>
	<form action="MyServlet" method="get">
		UserName:<input type="text" name="username">
		<br/>
		Password:<input type="password" name="password">
		<br/>
		<input type="submit"/>
	</form>
</body>
</html>

 

然后在tomcat上运行部署

在浏览器内输入http://localhost:8080/工程

ok了。。。

posted @ 2012-11-05 20:35  tf_swufe  阅读(2697)  评论(1编辑  收藏  举报