JavaWeb学习笔记1——第一个简单的JavaWeb项目

首先打开MyEclipse,并在菜单上选择File—New—Other,在弹出的对话框中选择Web Project,然后Next,随后在弹出的对话框中填写Project Name:firstWeb,到此已经建立好了一个JavaWeb项目,名为firstWeb。

然后需要在该项目下新建一个Servlet。方法如下,选择File—New—Other,在弹出额度对话框中找到Web下面的Servlet,单击Next,在弹出的对话框中输入package名称:com.test.secondWeb(也可以根据自己习惯改为别的名称),Servlet名称:HelloServlet,然后Finish。

到这里准备工作已经完成,下面是刺激的代码环节。

MyEclipse自动生成的HelloServlet.java只是完成了一局简单话的输出,这里我们需要自己进行修改源代码。

 

package com.test.secondWeb;

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;

public class HelloServlet extends HttpServlet {

	/**
		 * Constructor of the object.
		 */
	public HelloServlet() {
		super();
	}

	/**
		 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * 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 { response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println(""); out.println("A Servlet"); out.println(" "); String requestURI=request.getRequestURI(); out.print("
"); out.print("请输入您的名字:"); out.println(""); out.println("
"); out.println(""); String name =request.getParameter("name"); if(name!=null&&name.trim().length()>0){ out.println("您好,"+name+".欢迎来到Java Web世界."); } out.println(" "); out.println(""); out.flush(); out.close(); } /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @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 doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }

代码完成,想要看到项目运行效果还需要进行吧项目部署到Tomcat上,MyEclipse2017部署方法(如果其他版本略有不同请自行找度娘吧):菜单上选择windows—show view—Servers,这是MyEclipse下方位置会弹出Servers框,在你自己安装的Tomcat上()右键选择Add/Remove Deployments···,然后选择项目firstWeb,点击add,完成关闭对话框。

 

然后就是看看项目效果了,首先启Tomcat,方法是在刚才你自己安装的Tomcat上右键选择Strat,然后等待一会儿(这时代码框下面会有一些文字不断刷新,等到一切回归风平浪静就ok了,大概也就5、6秒钟,具体看电脑配置和项目大小吧)。

然后打开MyEclipse自带的浏览器,如图:

然后在地址框中输入:http://localhost:8080/firstWeb/servlet/HelloServlet  回车即可看到效果图

可以在文本框中输入你的名字,点击“提交查询内容”,最后效果如图:

JavaWeb学习笔记1——第一个简单的JavaWeb项目_第1张图片

你可能感兴趣的:(JavaWeb)