1. 程式人生 > >MyEclipse配置Tomcat 並編寫第一個JSP程式

MyEclipse配置Tomcat 並編寫第一個JSP程式

安裝myeclipse之後配置tomcat伺服器,在window裡選擇servers


選擇tomcat的資料夾路徑(我的是從別人那裡考過來的資料夾)


選中上enable即可


出現了這個介面


在這裡可以開啟或關閉伺服器

開啟伺服器之後可以開啟瀏覽器看看 tomcat伺服器有沒有開啟

網址為http://localhost:8080/


然後新建一個網路工程叫 myJsp


然後就有以下這些


Open with 可以開啟圖形化的開發拖拽介面


然後右鍵new servlet程式

Jsp是顯示介面 view

Servlet轉交使用者的請求 control

Java程式  是  model

mvc架構

寫完程式之後開啟網址

http://localhost:8080/myJSP/

 在index.jsp裡面編寫程式,這是一個可以在

在瀏覽器端輸入姓名,在java控制檯中顯示所輸入的姓名程式

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> 
    This is my JSP page. <br>
    <font color="00#101">你好<font size="5">帥</font>!!!</font>
    <form action="testservlet" method="post">
    <table>
    <tr><td>姓名</td><td><input type="text" name="name"></td></tr>
    <tr><td>密碼</td><td><input type="text" name="pwd"></td></tr>
    <tr><td><input type="submit" value="提交"></td></tr>
    </table>
    </form>
   <br></body>
</html>

經過 servlet傳遞

 servlet關鍵程式為

public void doPost(HttpServletRequest request, HttpServletResponseresponse)

           throws ServletException, IOException {

       String i = request.getParameter("name");

       test a = new test();//與下面java程式有關 

       a.wawa(i);

    }

在console就能得到

在java程式中輸出這個值

 java程式為

public class test

{

    public void wawa(String q){

       System.out.print("姓名是"+q);

    }

}

把這個工程部署到jsp伺服器上 選adddeployment


選工程名


輸入姓名按提交

 

如果在別人的電腦上訪問

如果新增了其它的jsp檔案,直接在路徑後加上檔名,比如

http://你的ip地址:8080/myJSP/main.jsp