jsp基础速成精华讲解
Servlet三个要素:
1.必须继承自HttpServlet
2.必须实现doGet()或者doPost()
3.必须在web.xml中配置Servlet
<servlet>
<servlet-name> </servlet-name>
<servlet-class> </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> </servlet-name>
<url-pattern> </url-pattern>
</servelt-mapping>
HttpServeltRrequest:请求对象
getParameter():获得表单元素的值
getAttribute():获得request范围中的属性值
setAttribute():设置reqeust范围中的属性值
setCharacterEncoding():设置字符编码
HttpSerletResponse:相应对象
sendRedirect():外部跳转
getWriter():获得输出流对象
setContentType("text/html; charset=utf-8"):设置相应内容格式和编码
四种会话跟踪方式:
1.Session
HttpSession session = request.getSession();
session.setAttribute("name", "zhangsan");
session.setAttribute("pwd", "aaa");
String name = (String) session.getAttribute("name");
2.cookie:
//创建Cookie
Cookie cookie = new Cookie("name", "zhangsan");
//设置Cookie的超时时间
cookie.setMaxAge(24 * 60 * 60 *60);
//把Cookie发送到客户端
response.addCookie(cookie);
//得到客户端发送的Cookie
Cookie [] cookies = request.getCookies();
for(int i=0; i <cookies.length; i++) {
Cookie temp = cookies;
String key = temp.getName();
String value = temp.getValue();
}
3.隐藏表单域
<input type="hidden" name="name" value="zhangsan" />
request.getParameter("name");
4.Url重写
问号传参
LoginServlet?username=zhangsan&pwd=123
String name = request.getParameter("username");
String pwd =request.getPareameter("pwd");
内部跳转:
LoginServlet
request.getRequestDispatcher("index.jsp").forward(request, resposne);
外部跳转:
response.sendRedirect("index.jsp");
内部跳转是一次请求和一次响应
外部跳转是两次请求和两次响应
ServletContext:Servlet上下文对象
它是一个公共区域,可以被所有的客户端共享
setAttribute():向公共区域里放入数据
getAttribute():从公共区域里取
相关文档:
一,纯粹的jsp页面中文乱码
原因:对jsp代码的编写,默认的字符集事“ISO-8859-1”,如果代码中存在中文,则会出现乱码!
解决办法:在page中加上contentType="text/html;charset=gb2312",采用国标来翻译页面中的代码!
二,post方式提交表单出现乱码
原因: ......
自己分页,下一篇会介绍使用diasplaytag组件。
使用mysql5, 有个数据表user,字段有id, username, password。
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional// ......
此种方法使用excel的组件,要求客户端必须装有excel程序。方法是将表格中的每个td标签的内容取出,作为excel文件中一个单元格的内容。
1、在网页中添加java脚本
function AutomateExcel(tableid,unusecolumn)
{
// 获取excel组件
var oXL = new ActiveXObject("Excel.Application");
// ......
一、jsp连接Oracle8/8i/9i数据库(用thin模式)
testoracle.jsp如下:
< %@ page contentType= "text/html;charset=gb2312 "% >
< %@ page import= "java.sql.* "% >
< html >
< ......
系统环境:Linux 5.0 AS 64位
步骤:
1、下载apache-tomcat-5.5.23.tar和jdk-1_5_0_12-linux-i586.bin并存放在/usr/local文件夹下;
2、安装jdk
[root@localhost local]#chmod 775 jdk-1_5_0_12-linux-i586.bin '非必须,要是直接运行时提示权限不够时改变权限;
[root@localhost local]#./jd ......