Java web开发快速上手

学习编程,必须learning by doing。

学习编程,千万不要被某些莫名其妙的细节搞得失去学习的兴趣

这里给同学们一个最快速的Java web上手例子。初学Java Web开发时,先什么都不需要懂(《编程导论(Java)》除外,讨论Web开发时,你还敢问Java编程的基础问题),代码跑起来最大!

Java web开发快速上手_第1张图片

使用FreeMind的直接按照上图学习,复制其中附带的代码。

1.开发环境

初学Java Web开发时,yqj2065要求你省略掉一切多余的东西。JSP、xml文件用记事本,servlet用BlueJ,服务器用Tomcat解压版。

在你学习《编程导论(Java)》时,已经安装了JDK、BlueJ的基础上,JDK环境变量仅需设置一下classpath;

Tomcat的环境变量
(1)变量名: CATALINA_BASE     变量值: D:\JavaWeb\apache-tomcat-7.0.64(Tomcat解压到的目录)
(2)变量名: CATALINA_HOME     变量值: D:\JavaWeb\apache-tomcat-7.0.64
(3)变量名: CATALINA_TMPDIR     变量值:D:\JavaWeb\apache-tomcat-7.0.64\temp
(4)变量名: Path                                  变量值:D:\JavaWeb\apache-tomcat-7.0.64\bin

cdm-startup.bat启动服务,点击http://localhost:8080/能够看见默认的页面为准。

BlueJ:需要将Tomcat自带的servlet-api.jar复制一份丢到BlueJ的BlueJ\lib\userlib中即可,编译servlet需要它。

2.HelloWorld

目标: 在客户端浏览器输出字符串。3步:

1)创建若干文件夹;编写基本(以后一直要在其中修改)的2)HelloWorld\index.jsp和3)HelloWorld\WEB-INF\web.xml。

你可以在tomcat\webapps\examples中提供的例子上修改,也可以用脑图中附带的例子。如index.jsp

<%@page language="java" pageEncoding="gbk"%>

Apache Tomcat Examples-HelloWorld
HelloWorld
web.xml如下:





    
        index.jsp
    

启动服务,点击http://localhost:8080/HelloWorld/(大小写敏感)。

3.增强HelloWorld

1. 编写HelloServlet。(默认情况下,Web应用HelloWorld使用的servlet,其类文件应该放在WEB-INF/classes中。如果类全名为com.myorg.MyServlet,它的路径必须为WEB-INF/classes/com/myorg/MyServlet.class。)

BlueJ在文件夹HelloWorld\WEB-INF创建项目classes。(一劳永逸)。复制粘贴下面的代码(马上会添加package语句),编译。

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;
/**
 * Write a description of class HelloServlet here.
 *
 * @author (yqj2065)
 * @version (0.1)
 */

 
/**
 * 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);
       }                   
}

2.输入界面:HelloWorld\login.jsp。一个文本框/ text 、密码框/password、按钮/input type="Submit"。 复制粘贴.没有排版,反正不是现在要看的。

action="HelloServlet",web.xml中取的某个名字,方便起见,采用类名.

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




helloapp



User Name:
Password:
<
 
3.输出使用页面:HelloWorld\hello.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>




helloapp


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

4.index添加超级链接到login页面

       

login     

6.web.xml配置servlet

    
      HelloServlet
      yqj2065.HelloServlet
    
    
        HelloServlet
        /abc
    
在浏览器中直接输入http://localhost:8080/HelloWorld/abc。之后, abc改回HelloServlet。

4.最后的任务

把Java web运行需要做的事情,变成2个超级链接。

你可能感兴趣的:(《编程导论(Java)》训&练)