Servlet+jsp+java Implementing Web Applications
Environment:
1,eclipse
2,tomcat
3,eclipse Tomcat Plugin
Development process:
1, build a dynamic Web Project
2. Create a Welcome page
Page can be jsp/html, we choose a JSP page (placed in webcontent)
<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 " pageencodingin? Select:<br> <select> <option>Yes <option>No </select> <center> <input type= "Submit" > </center></form ></body>
3. Add a servlet file to the project
Packagecom.example;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** Servlet Implementation class Welcome*/@WebServlet ("/welcome") Public classWelcomeextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; /** * @seeHttpservlet#dopost (httpservletrequest request, httpservletresponse response)*/ protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method StubResponse.setcontenttype ("text/html"); PrintWriter out=Response.getwriter (); String C= Request.getparameter ("select"); if(C.equals ("Yes")) Out.print ("Welcome!"); ElseOut.print ("I don ' t like you!"); //jump to another page with a parameterRequest.setattribute ("token", token); Request.getrequestdispatcher ("Welcome.jsp"). Forward (Request,response); }}
4. Create a welcome.jsp page and accept the parameter values
<%@ 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" >// %><style> #ifr_fr { 84%; height:700px; } </style> Welcome to RZZX1. <iframe id= "Ifr_fr" name= "ifr_fr" frameborder= "0" src= "" ></iframe></body>
5, create a Web. xml
Web. XML is used to establish the servlet-JSP relationship (which needs to be placed within the Web-inf).
Different servlets are invoked according to different URLs for processing.
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version= " 2.5 "> <display-name></display-name> <servlet> <servlet-name>Welcome< /servlet-name>//the location of the class to be called with the same name as the following <servlet-class>com.example.welcome</servlet-class>// </servlet> <servlet-mapping> <servlet-name>Welcome</servlet-name> < Url-pattern>/hello.do</url-pattern>//URL id </servlet-mapping></ Web-app>
6, test address: http://localhost:8080/webtest/hello.do
7, you may be prompted: HTTP method GET is not supported by the This URL
Solution:
voidvoid Service (Httpservletr ...
Servlet+jsp+java Implementing Web Applications