It has been a long time since we finished learning Jsp. In the process, we summarized a little bit of basic knowledge and shared it with beginners. Until we finally made a small project of ours, a pet hospital, we also used our simplest tool Jcreater4. 0, haha, I only found out after using Ecliplse that the development speed is so fast, but this also laid our foundation... I won’t say more, just send a small summary of what I learned here. We are willing to bring convenience to beginners, and at the same time, we hope that experts can point out the shortcomings...
First, let's talk about our jsp course: the first part is Servlet technology, including Servelt introduction, session tracking, JavaMal and Servlet. Chapter 2 The second part is JSP technology, including: jsp introduction, use of jsp scripts and instructions, jsp implicit objects, JavaBeans and standard actions in jsp, jsp expression language, custom tags, jsp custom tag library. The third part It is the filter design pattern, including filter and MVC design pattern.
1. Servlet knowledge and common errors and rules.
1. The process of desktop running program and WEB application is essentially the same---based on the process of request and response.
2.http protocol (hypertext transfer protocol)
1). Stateless: There is no connection between multiple requests for information.
2). Used to send request and response messages over the Internet
3). Use the port to receive and send messages, the default is port 80.
Port: for memory buffer (multi-threaded receiving data)
windows: 0---port 65535
0---4096 is the system port
3.What is Servlet?
Server let server-side applet.
A program written in Java that is used to process requests sent by the client and have the server respond to the client.
Servlet only runs on the server
4. Detailed explanation of Servlet:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class MyServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throwsServletException,IOException
{
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("gb2312");
response.sendRedirect("Login.jsp"); //Redirect method
request.getParameter(""); //Read client data
//The following method is forwarding. What is different from redirection is that the data is not lost during forwarding.
ServletContext context=this.getServletContext();
RequestDispatcher dispatcher=context.getRequestDispatcher("/welcome.jsp");
dispatcher.forward(request,response);
...//The following methods are included (relatively rarely used)
dispatcher.include(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throwsServletException,IOException
{
doGet(request,response);
}
}
(2). When you need to pass parameters to the Servlet, Src="Servlet?name=' ' ";
5. When executing the servlet, a download prompt occurs:
(1). Possibility one: text/html;charset=GBK The semicolon in the middle is written as a comma.
(2). Possibility two: The configuration information in XML is incorrect.
(3) Possibility three: When defining global variables, for example: CONTENT_TYPED should be placed in double quotes for future use.
(4). Possibility four: When the wrong characters are written in the text/html and charset above, the download will also be prompted.
6. When executing the servlet, a 404 error occurs:
(1). Mostly due to configuration errors in xml.
(2). There may be an error in the URL in the servlet communication method.
(3).When the form is submitted: action path.
(4). A relatively stupid mistake is that the mark is written incorrectly, please check carefully.
7. When executing the servlet, a 403 error occurs:
(1). An error occurs in the site under tomcat.
8. When executing the servlet, a 405 error occurs:
(1) ). When there is no post submission method in the servlet.
95. When executing the servlet, a 500 error occurs:
(1). Most of them are abnormal errors in the servlet program code.
10. During execution, the data taken out is null. It must be based on the actual situation Find the corresponding error message.
11. When executing the jsp page, a 500 error message occurs....
Most of them are compilation errors during the translation process of jsp pages! It is a serious error. You can follow the prompts to go back and find it...
2. XML configuration
<!--Configuration information in config, the configuration needs to be in the xml in the servlet -->
<servlet>
<init-param>
<param-name>sess</param-name>
<param-class>com.serv<param-class>
</init-param>
<servlet-name>Myservlet</servlet-name>
<servlet-class>com.Myservetl</servlet-class>
</servlet>
<!--This is mapping Myservlet-->
<servlet-mapping>
<servlet-name>Myservlet</servlet-name>
<servlet-class>/url</servlet-class>
<servlet-mapping>
<!--Configuration information in context-->
<context-param>
<param-name>ses</param-name>
<param-class>com.ser</param-class>
</context-param>
Note: Configuration information can only be read, and config can be accessed in a single Servlet, and context can be accessed globally
.
======================== Summary of session tracking technology ====================== ==
User authorization.
Hide form fields
URL rewriting
Cookie usage
-------------------------------------------------- ------------------------------------------
1. Session: It is multiple requests and responses between the same client and server at the same time.
2. Session usage (key points)
HttpSession session=request.getSession();
session.setAttribute("name",Object); //Attach value
session.getAttribute(); //Value
session.removeAttributer();
3.Cookie(class)
1).What are cookies?
A cookie is a string, supported by HTTP, that can be permanently saved and written to the client (hard disk).
With every request, space is left for cookies in the response.
2).Usage:
Cookie cookie=new Cookie("name",cookie); //The key and value of the cookie must be specified and must be a string.
response.addcookie(cookie);
cookie.setMaxAge(3600); //In seconds.
//Read the cookie sent by the client, the return value type is: cookie array
request.getCookies();
//Loop to read keys and values.
Usage process: (1). Generate cookie, new cookie("","")
(2).Set lifetime >0,SetMaxAge(seconds).
(3). Send to the client: response.addCookie(cookie);
Tips: (1). A website can write up to 20 cookies to a client.
(2). A client can receive up to 300 cookies.
4. The relationship between Session and Cookie:
The session ID is passed between the client and the server as the cookie value.
-------------------------------------------------- ------------------------------------------
4. Principles for constructing entity beans:
/*
* 1. The bean class is public
* 2. Class members are private
* 3. There must be a parameterless structure
* 4. There are set() and get() methods
* 5. The method is named setXxx() or getXxx() method
*/
5. MVC implements data encapsulation
. This data encapsulation standard is summarized according to MVC.
Required content: 1. Servlet 2. JavaBean 3. OperBean (DBconnection) 4. Jsp
extracts the foreground data from Servlet, then encapsulates the data in JavaBean, and then instantiates it. OperBean, call methods in OperBean,
Pass the JavaBean object as a parameter and perform a series of operations, then store the returned data in an ArrayList or other collection, and encapsulate the collection object in the Session object to facilitate value retrieval in the foreground.
Servlet control:
... .....
Arraylist lis=new ArrayList();
String name=request.getParameter("username");
JavaBean bean=new JavaBean();
bean.setName(name);
OperBean oper=new OperBean();
lis=oper.operMethod(bean);
HttpSession session=request.getSession();
session.setAttribute("list",lis);
OperBean logic:
...
public ArrayList OperMethod(JavaBean bean)
{
ArrayList lis=new ArrayList();
String usr=bean.getName("name");
String sql="select * from student where name='"+usr+"'";
DBconnection db=new DBconnection();
...//Operation data business
return lis;
}
Front desk business:
...
ArrayList list=(ArrayList)session.getAttribute("lis");
Iterator ite=list.iterator();
while(ite.harNext()) //Traverse the output
{
JavaBean bean=(JavaBean)ite.next();
.
//Get the bean median value
}
//From this, you can perform operations such as "user login", "data addition, deletion, modification and query", "logout and login"...
All the above jsp technologies are not related to struts and other frameworks, but are simple MVC ideas. Only by understanding the above simple ideas can we further delve into high-level things such as frameworks. I feel so. After all, I am also a beginner. There is still a lot of knowledge that I don’t understand, and I am constantly learning. If there is any If any friends have good opinions or good learning methods, please give them some advice!