java webå ¥é¨--001
程序员文章站
2024-01-30 20:21:34
...
ï¼1ï¼æ°å»ºä¸ä¸ªjava å·¥ç¨ï¼
ï¼2ï¼ç¶ååæ ¹ç®å½ä¸æ°å»ºæ件夹WebContent,并形æå¦ä¸ç»æï¼
1--WebContent(ææä¸javawebæå ³çé½æ¾å¨è¿éé¢)
2--WEB-INF(å¿ é¡»è¦å»ºç«ä¸ä¸ªè¿æ ·çæ件夹ï¼ååä¹å¿ é¡»ä¸æ¨¡ä¸æ ·)
3--classes(å¿ é¡»å»ºç«è¿æ ·ä¸ä¸ªååclassesçæ件夹ï¼ç¨æ¥æ¾ç½®ç¼è¯å¥½çclassæ件)
3--libï¼éè¦çjarå ï¼
3--web.xmlï¼webé 置信æ¯ï¼
2--test.jspï¼é¡µé¢å±ç¤ºä¿¡æ¯ï¼
2--jsp页é¢..
2--html页é¢..
2--å¾ç..
ï¼3ï¼å¨srcä¸é¢æ°å»ºä¸ä¸ªç±»Person
注ï¼å¯ä»¥éè¿ä¿®æ¹é»è®¤çè¾åºç®å½ä»¥è¾¾å°å¯ä»¥èªå¨å°å°ç¼è¯å¥½çclassæ¾ç½®å°classesæ件夹ä¸ï¼
å ·ä½ä¸ºå·¥ç¨å³é®-->build path-->Configure Build Path-->Source-->ç¶åå°é»è®¤ç®å½æ´æ¹æ为classesç®å½å³å¯ã
ï¼5ï¼å¨WebContentç®å½ä¸æ°å»ºä¸ä¸ªjspæ件å½å为test.jsp,å¦ä¸æ示ï¼
æ¾ç½®Personç±»çæ¶åï¼å¦æ没ææ示çè¯å¯ä»¥æAlt+/æ¥èªå¨å¼å ¥è¯¥ç±»ï¼
ï¼6ï¼web.xmlæ件为
注æï¼å¦æéè¿é ç½®æ件é ç½®çè¯åä¸éè¦æå·¥å°å¤å¶äºã
é ç½®æ件æä¹é ç½®å¢ï¼å¯ä»¥æ°å»ºä¸ä¸ªxmlæ件ï¼ä¾å¦helloword2.xmlï¼ç¶åå¨éé¢é ç½®å¦ä¸ï¼
å ¶ä¸docBase为WebContentççå®ç©çè·¯å¾ï¼path为该javawebçä¸ä¸ªå¯ä¸æ è¯ã并没æä»ä¹ç¨ï¼å¯æå¯å æãï¼reloadable为trueå³ä¸ºæ¯æ¬¡æ´æ¹ç±»classçæ¶åé½ä¼éæ°ç¼è¯ï¼å¹¶éæ°å è½½ã
ç¶åå°helloword.xmlæ¾å¨C:\Program Files (x86)\Java\apache-tomcat-6.0.44\apache-tomcat-6.0.44\conf\Catalina\localhost ç®å½ä¸ç¶åå°±å¯ä»¥éè¿
http://localhost:8989/helloword2/test.jsp ã注æè¿éçhelloword2å³ä¸ºxmlé ç½®æ件çååã
ï¼8ï¼ç¶åå访é®http://localhost:8989/helloworld/test.jsp
å°±å¯ä»¥å¨tomcatçdosçªå£ä¸çå°èªå¨æå°çPersonä¿¡æ¯ï¼
Person [name=null, password=null]
ï¼2ï¼ç¶ååæ ¹ç®å½ä¸æ°å»ºæ件夹WebContent,并形æå¦ä¸ç»æï¼
1--WebContent(ææä¸javawebæå ³çé½æ¾å¨è¿éé¢)
2--WEB-INF(å¿ é¡»è¦å»ºç«ä¸ä¸ªè¿æ ·çæ件夹ï¼ååä¹å¿ é¡»ä¸æ¨¡ä¸æ ·)
3--classes(å¿ é¡»å»ºç«è¿æ ·ä¸ä¸ªååclassesçæ件夹ï¼ç¨æ¥æ¾ç½®ç¼è¯å¥½çclassæ件)
3--libï¼éè¦çjarå ï¼
3--web.xmlï¼webé 置信æ¯ï¼
2--test.jspï¼é¡µé¢å±ç¤ºä¿¡æ¯ï¼
2--jsp页é¢..
2--html页é¢..
2--å¾ç..
ï¼3ï¼å¨srcä¸é¢æ°å»ºä¸ä¸ªç±»Person
package com.cmbc.model;
public class Person {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String name;
private String password;
@Override
public String toString() {
return "Person [name=" + name + ", password=" + password + "]";
}
}
ï¼4ï¼æå·¥å°å°Person类对åºçclassæ件ï¼å«å
ï¼å¤å¶å°classesæ件夹ä¸ï¼æ³¨ï¼å¯ä»¥éè¿ä¿®æ¹é»è®¤çè¾åºç®å½ä»¥è¾¾å°å¯ä»¥èªå¨å°å°ç¼è¯å¥½çclassæ¾ç½®å°classesæ件夹ä¸ï¼
å ·ä½ä¸ºå·¥ç¨å³é®-->build path-->Configure Build Path-->Source-->ç¶åå°é»è®¤ç®å½æ´æ¹æ为classesç®å½å³å¯ã
ï¼5ï¼å¨WebContentç®å½ä¸æ°å»ºä¸ä¸ªjspæ件å½å为test.jsp,å¦ä¸æ示ï¼
<%@page import="com.cmbc.model.Person"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Person person = new Person();
System.out.println(person);
%>
</body>
</html>
jspå°±æ¯æ¢å¯ä»¥åjava代ç åå¯ä»¥åhtml页é¢çä¸è¥¿ãæ¾ç½®Personç±»çæ¶åï¼å¦æ没ææ示çè¯å¯ä»¥æAlt+/æ¥èªå¨å¼å ¥è¯¥ç±»ï¼
ï¼6ï¼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_2_5.xsd"
version="2.5">
</web-app>
ï¼7ï¼æåæWebContentç®å½å¤å¶å°tomcatçwebappsç®å½ä¸ï¼å¹¶æ¹å为hellowordã注æï¼å¦æéè¿é ç½®æ件é ç½®çè¯åä¸éè¦æå·¥å°å¤å¶äºã
é ç½®æ件æä¹é ç½®å¢ï¼å¯ä»¥æ°å»ºä¸ä¸ªxmlæ件ï¼ä¾å¦helloword2.xmlï¼ç¶åå¨éé¢é ç½®å¦ä¸ï¼
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="G:\\Study_WorkSpace\\TomcatTest\\WebContent" path="/test2"
reloadable="true" />
å ¶ä¸docBase为WebContentççå®ç©çè·¯å¾ï¼path为该javawebçä¸ä¸ªå¯ä¸æ è¯ã并没æä»ä¹ç¨ï¼å¯æå¯å æãï¼reloadable为trueå³ä¸ºæ¯æ¬¡æ´æ¹ç±»classçæ¶åé½ä¼éæ°ç¼è¯ï¼å¹¶éæ°å è½½ã
ç¶åå°helloword.xmlæ¾å¨C:\Program Files (x86)\Java\apache-tomcat-6.0.44\apache-tomcat-6.0.44\conf\Catalina\localhost ç®å½ä¸ç¶åå°±å¯ä»¥éè¿
http://localhost:8989/helloword2/test.jsp ã注æè¿éçhelloword2å³ä¸ºxmlé ç½®æ件çååã
ï¼8ï¼ç¶åå访é®http://localhost:8989/helloworld/test.jsp
å°±å¯ä»¥å¨tomcatçdosçªå£ä¸çå°èªå¨æå°çPersonä¿¡æ¯ï¼
Person [name=null, password=null]