1、当服务器响应客户端之后,request和response会立即销毁,在客户端去访问服务器的时候就开启Servlet,当Servlet开启之后就会产生request和response。
2、访问Servlet的6种方式:通过地址栏输入网址提交访问(直接在浏览器上写网址访问),表单访问,超链接访问,伪链接访问,函数访问,用Java代码访问(不讲)。
①地址栏输入网址提交访问:http://ip:port/项目名称/user.do<后面可以追加参数,别忘了追加前加一个’?’> ===>其中,user.do也可以写成user.action。
其实就是get方法访问,这种访问方法出现的问题为:乱码,参数在地址栏可以看见且参数大小不能超过255个字符,不安全。
②表单:post提交方式无乱码,以后用表单方式访问时,统统采用post方式提交。
<!-- JSP -->
<body>
<form action="user.do" method="post">
用户名:<input type="text" name="name" /><br />
密 码:<input type="text" name="pwd" /><br />
<input type="submit" value="提交" /><br />
</form>
</body>
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;
//sql
public class UserServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.setContentType("text/html;charset=UTF-8");
String name = request.getParameter("name");// 取表单数据或地址栏参数的方式。
String age = request.getParameter("age");
// if(name!=null) // 解决乱码方式,如果name的值不为空就执行下面的语句,如果为空,就不作任何处理
// {
// name = new String (name.getBytes("ISO-8859-1"),"UTF-8");
// }
System.out.println(name);
request.setAttribute("myname", name);// 通过键“myname”,显示(浏览器取出)值name
request.setAttribute("myage", age);
request.getRequestDispatcher("user.jsp").forward(request, response); // 转发
}
}
3、实际开发中,转发技术用的较多。
request.getRequestDispatcher("user.jsp").forward(request, response); // 转发
4、通过键“name”,显示(浏览器取出)值“小明”。
request.setAttribute("name","小明");
5、取表单数据或地址栏参数的方式:
String name = request.getParameter("name");
String age = request.getParameter("age");
6、可以用${ }表达式,显示参数的内容,例如:${myname} ${myage} <通过键取值>。
request.setAttribute("myname",name);
request.setAttribute("myage",age);
7、多页面跳转<其实是通过Servlet转发>
不通过Servlet进行转发的<这种做法在实际中不用,违背了MVC开发模式,且页面风格、代码风格不统一,难以维护。>
package com.fs.servlet;
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 StuServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String to = request.getParameter("to");// 取表单数据或地址栏参数的方式。
if("login".equals(to)) {
this.login(request, response);
}else if("main".equals(to)) {
this.main(request, response);
}
}
//转发到main.jsp
private void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("main.jsp").forward(request, response);
}
//转发到login.jsp
public void login(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
8、实际中采用MVC开发模式。V显示:jsp,C控制:Servlet
9、会话用对象session<键值对>存储。session对象就是会话对象。只要涉及到登陆的网站就会用上session技术。session和request一样,可以装东西。
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class UserServlet extends HttpServlet {
public int no=100;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
no ++;
String to = request.getParameter("to");
if("login".equals(to)){
this.login(request, response);
}else if("one".equals(to)) {
this.one(request, response);
}
}
private void one(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//转发到one.jsp
request.getRequestDispatcher("one.jsp").forward(request, response);
}
public void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
//使用数据验证name与pwd是否正确
if("123".equals(pwd)) {
HttpSession session = request.getSession();
session.setAttribute("username", name);
// request.setAttribute("username", name);
request.setAttribute("no", no+"");
request.getRequestDispatcher("main.jsp").forward(request, response);
}else {
request.setAttribute("inf", "用户名或密码错误!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
}
10、用session对象装数据的方式,在存储转发过程中,数据不会丢失,session会一直在。(多线程的时候会在服务器里面产生多个session对象,且在服务器里保存,各个session对象会根据你的(sessionID===>流水号(长整型))信息进行控制,当关闭浏览器的时候session会自动销毁)
11、设置超链接标签:<a></a> ====>主要属性参数:href="...",例如下面的代码
<a href="user.do?to=login">到login.jsp</a>