您的位置:首页 > 编程语言 > Java开发

Java web开发快速上手

2015-09-27 17:21 701 查看
学习编程,必须learning by doing。学习编程,千万不要被某些莫名其妙的细节搞得失去学习的兴趣

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



使用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的lueJ\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"%>
<html>
<head><title>Apache Tomcat Examples-HelloWorld</title></head>
<body>HelloWorld</body>
</html>
web.xml如下:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>
启动服务,点击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"%>
<!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>
3.输出使用页面:HelloWorld\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>
4.index添加超级链接到login页面

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

6.web.xml配置servlet

    <servlet>
      <servlet-name>HelloServlet</servlet-name>
      <servlet-class>yqj2065.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/abc</url-pattern>
    </servlet-mapping>
在浏览器中直接输入http://localhost:8080/HelloWorld/abc。之后,abc改回HelloServlet。

4.最后的任务

把Java web运行需要做的事情,变成2个超级链接。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: