这几天过年呆家各种串门, 被搞得半死, 不过平常有空还是看看java, 现在把这几天学习的东西总结一下, 主要是javaWEB的代码:
javaWEB的servlet是容器, webRoot(webContent)这个目录是服务器对外的根目录,这个目录下的所有文件都可以通过web直接访问,这个目录也就是我们常说的VIEW(视图层);
如果我们新建了servlet, 如何新建一个servlet可以参考这里, eclipse会自动新建一个指向该servlet的映射, servlet默认会引入几个java文件
//这个很重要
import java.io.PrintWriter;
//IO异常和servlet异常
import java.io.IOException;
import javax.servlet.ServletException;
//annotation是声明的意思;
import javax.servlet.annotation.WebServlet;
//
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(description = "test0", urlPatterns = { "/test0" }) ;这句话的意思是所有访问 /test0 的patterns, 都会调用这个servlet;
@WebServlet(name = "controllerServlet", urlPatterns = {"/product_input", "/product_save"}); 这句话的意思是所有访问 /product_input, /product_save 的patterns, 都会调用这个servlet, 在这个servlet里面直接判断url然后对每一种地址做对应的返回, 这个就是controller(控制器) ;
MVC现在还差Model了, Model就是数据层面的增删改查, 根据用户的操作(GET,POST或者其他请求方式)我们返回对应的Model给用户,
新建出来的servlet.java文件默认有 doGet, doPost, 还有init 以及destory等默认的实例公用非法,init方法里面可以放初始化的代码, 没有为空就可以了 ,比如:
public void init() throws ServletException {
london = new ArrayList<String>(10);
paris = new ArrayList<String>(10);
london.add("buckingham palace");
london.add("london Eye");
london.add("British Museum");
london.add("national Gallery");
paris.add("Eiffel Tower");
paris.add("notre Dame");
paris.add("the louvre");
paris.add("champs elysees");
}
doGet或者doPost的两个参数分别为HttpServletRequest request, HttpServletResponse response;
HttpServletRequest 有这些方法:
1:getAuthType
2:getCookies
3:getDateHeader
4:getHeader
5:getHeaderNames
6:getIntHeader
7:getMethod
8:getPathInfo
9:getPathTranslated
10:getQueryString
11:getRemoteUser
12:getRequestedSessionId
13:getRequestURL
14:getServletPath
15:getSession
16:isRequestedSessionIdValid
HttpServletResponse的方法
flushBuffer
getBufferSize
getCharacterEncoding
getContentType
getLocale
getOutputStream
getWriter
isCommitted
reset
resetBuffer
setBufferSize
setCharacterEncoding
setContentLength
setContentType
setLocale
addHeader
sendError
sendError
setHeader
setStatus
addCookie
一个doGet返回的简单例子:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.write( "<form method='post'>" +
"<table>"+
"<tr>" +
"<td>" +
"<select name='titleFontSize'>" +
"<option>large</option>" +
"<option>xlarge</option>" +
"<option>xxlarge</option>" +
"</select>" +
"</td>" +
"<td><input type='submit' value='set' /></td>" +
"</tr>" +
"</table>" +
"</form>" );
writer.flush();
writer.close();
}
提供一个例子大概了解了javaBean是什么东东, 有两个模型(UserBean.java 和 UserCheckBean.java), login.jsp. loginchk.jsp两个jsp文件:
UserBean.java
package user.model;
public class UserBean {
private String name;
private String password;
public String getName () {
return this.name;
}
public void setName( String name ) {
this.name = name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserCheckBean.java
package user.model;
public class UserCheckBean {
protected UserBean user;
public UserCheckBean () {
}
public UserCheckBean( UserBean user ) {
this.user = user;
}
public UserBean getUser () {
return user;
}
public void setUser( UserBean user ){
this.user = user;
}
public boolean validate() {
String name = user.getName();
String password = user.getPassword();
if( "x".equals(name) && "x".equals(password) ) {
return true;
}else{
return false;
}
}
}
login.jsp
<%@ 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>
<form method="POST" action="longinchk.jsp">
name : <input type="text" name="name" />
password : <input type="text" name="password" />
<input type="submit" value="submit" />
</form>
</body>
</html>
loginchk.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!--
因为eclipse会自动检测错误, 引用路径错误了会自动提示错误等, 没有写js那样,要各种调试, 都不知道哪里错误了;
-->
<%@ page import="user.model.UserCheckBean" %>
<!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>
<%
request.setCharacterEncoding("GB2312");
%>
<jsp:useBean id="user" scope="session" class="user.model.UserBean" />
<jsp:setProperty name="user" property="*" />
<%
UserCheckBean uc = new UserCheckBean( user );
if( uc.validate() ) {
out.println("登陆成功");
} else {
out.println("wrong user or wrong password!");
}
%>
</body>
</html>
还有一个javaBean的例子,包括一个javabean文件,一个reg.html和reg.jsp,显示用户信息的userInfo.jsp:
bean.java
package beans;
import java.io.Serializable;
public class bean implements Serializable{
private String name;
private int sex;
private String education;
private String email;
public String getName () {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return this.sex;
}
public void setSex( int sex ) {
this.sex = sex;
}
public String getEducation() {
return this.education;
}
public void setEducation( String education ) {
this.education = education;
}
public String getEmail () {
return this.email;
}
public void setEmail (String email) {
this.email = email;
}
}
reg.html;
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="reg.jsp" method="post" >
<div>
name : <input name="name" />
<div>
sex : <input type="radio" name="sex" value="1"/>男
<input type="radio" name="sex" value="0"/>女
<div>
eduction :
<select type="" name="education">
<option value="高中">高中</option>
<option value="大学">大学</option>
<option value="硕士">硕士</option>
<option value="博士">博士</option>
</select>
<div>
email : <input name="email" />
</div>
<div>
<input type="submit" value="submit"/>
</div>
</form>
</body>
</html>
reg.jsp
<%@ 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">
<%
request.setCharacterEncoding("GB2312");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="user" scope="session" class="beans.bean" />
<jsp:setProperty name="user" property="*" />
注册成功
</body>
</html>
userInfo.jsp
<%@ 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>
<jsp:useBean id="user" scope="session" type="beans.bean"></jsp:useBean>
你的姓名<jsp:getProperty name="user" property="name" /> </br>
你的性别<%
int sex = user.getSex();
if( 1 == sex ) {
out.println("男");
}else{
out.println("女");
};
%></br>
你的学历<jsp:getProperty name="user" property="education" /></br>
<%
out.println( user.getEducation() );
%>
你的email<jsp:getProperty name="user" property="email" />
</body>
</html>
天道酬勤