怎么用java实现html代码

html-css0102

怎么用java实现html代码,第1张

ava要运行html代码,需要运行在服务器端,也就是servlet容器中,经过容器编译解析,返回html静态内容,示例如下:

在servlet里面写就可以了

引入一系列包

import java.io.IOException

import java.io.PrintWriter

import javax.servlet.ServletException

import javax.servlet.ServletRequest

import javax.servlet.ServletResponse

import javax.servlet.http.HttpServlet

import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpServletResponse

import java.io.*

public class Servlet1 extends HttpServlet {

public void doGet(ServletRequest req,ServletResponse res)throws ServletException, IOException{try{PrintWriter pw=res.getWriter()//在浏览器输出需要

pw.println("<script<script")}catch(exception e){="" e.printstacktrace()="" 为发现调试错误}}}=""

如下:

public static String do_post(String url, List<NameValuePair> name_value_pair) throws IOException {

        String body = "{}"

        DefaultHttpClient httpclient = new DefaultHttpClient()

        try {

            HttpPost httpost = new HttpPost(url)

            httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8))

            HttpResponse response = httpclient.execute(httpost)

            HttpEntity entity = response.getEntity()

            body = EntityUtils.toString(entity)

        } finally {

            httpclient.getConnectionManager().shutdown()

        }

        return body

    }

    public static String do_get(String url) throws ClientProtocolException, IOException {

        String body = "{}"

        DefaultHttpClient httpclient = new DefaultHttpClient()

        try {

            HttpGet httpget = new HttpGet(url)

            HttpResponse response = httpclient.execute(httpget)

            HttpEntity entity = response.getEntity()

            body = EntityUtils.toString(entity)

        } finally {

            httpclient.getConnectionManager().shutdown()

        }

        return body

    }

java处理html指定标签最好用正则表达式。例如要去除html中所有的h1标签和类容就可以用下面的演示代码:

package konw.reg

import java.io.BufferedReader

import java.io.BufferedWriter

import java.io.FileNotFoundException

import java.io.FileReader

import java.io.FileWriter

import java.io.IOException

import java.util.regex.Matcher

import java.util.regex.Pattern

public class RemoveTag

{

    public static void main(String[] args)

    {

        FileReader fr

        String content =null

        String regex = "<[Hh]1>.*</[Hh]1>"

        try

        {

            fr = new FileReader("tag.html")

            BufferedReader br = new BufferedReader(fr)

            String str = null

            StringBuffer sb = new StringBuffer()

            while((str = br.readLine()) != null)

            {

                sb.append(str+"\n")

            }

            content = sb.toString()

            br.close()

            

        } catch (FileNotFoundException e)

        {

            e.printStackTrace()

        } catch (IOException e)

        {

            e.printStackTrace()

        }

        Pattern pattern = Pattern.compile(regex)

        Matcher matcher = pattern.matcher(content)

        StringBuffer sb1 = new StringBuffer()

        while(matcher.find())

        {

            sb1.append(matcher.replaceAll("")+"\n")

        }

        try

        {

            FileWriter fw = new FileWriter("tag.html")

            BufferedWriter bw = new BufferedWriter(fw)

            fw.write(sb1.toString())

            bw.close()

        } catch (IOException e)

        {

            e.printStackTrace()

        }

    }

}